0

I have this function that is supposed to open all text files in a folder and remove all the "\n" in it.

def FormatTXT():

    conhecimentos = os.listdir('U:/AutoCTE/Conhecimentos')

    for x in conhecimentos:
        with open(x, "r+") as f:
            old = f.read()
            text = old.replace("\n", "")
            f.seek(0)
            f.truncate(0)
            f.write(text)
            f.close()

But this function is returning the following error:

FileNotFoundError: [Errno 2] No such file or directory: '20200119-170415-Conhecimento de Transporte.txt'

Happens that this file actually exists in the directory and I can't figure out what I'm missing.

2
  • 1
    The file paths that you open in x are missing the prefix U:/AutoCTE/Conhecimentos. And since you are in a different directory, those relative paths will not work Commented Jan 31, 2020 at 17:07
  • Please post this as an answer, I can't believe I was this dumb hahaha, sorry Commented Jan 31, 2020 at 17:09

2 Answers 2

3

The file paths that you open in x are missing the prefix U:/AutoCTE/Conhecimentos. And since you are in a different directory, those relative paths will not work

def FormatTXT():

    conhecimentos = os.listdir('U:/AutoCTE/Conhecimentos')

    for x in conhecimentos:
        with open('U:/AutoCTE/Conhecimentos/' + x, "r+") as f:
            old = f.read()
            text = old.replace("\n", "")
            f.seek(0)
            f.truncate(0)
            f.write(text)
            f.close()

There are better ways to do this. For example with the os.path module

Sign up to request clarification or add additional context in comments.

2 Comments

what is the purpose of closing the file if you are using with?
There is not a purpose, he probably just copied code in the question.
1

I think the main problem you have is that you forgive to notice that os.listdir() return the name of the file in a directory not their path, you have to append the file name to the dir path using os.path.join()

There are several way to do this I will pick the 3 I use.

first let write a function that remove parse the file text because you get it right , I would just recommend caution using read() in case of very large file.

def remove_end_lines(file_): 
    """
    remove "\n" from file
    """
    with open(file_, "r+") as f:
        old = f.read()
        text = old.replace("\n", "")
        f.seek(0)
        f.truncate(0)
        f.write(text)

now we have to tackle your main problem file path. -> a choice could be to change the working dir (you should first register the original working dir in order to be able to go back to it)

def FormatTXT(my_dir):

    original_dir = os.getcwd() # register original working dir
    conhecimentos = os.listdir(my_dir) # liste file in the dir
    os.chdir(my_dir)  # change dir

    for file_ in conhecimentos: 
        remove_end_lines(file_)

    os.chdir(original_dir) # go back to original dir

second choice let's use os.path.join()

def FormatTXT(my_dir):

    conhecimentos = os.listdir(my_dir) # liste all files in the dir

    for file_ in conhecimentos: 
        file_path = os.path.join(my_dir, file_)  # create the file path by appening the file name to the directory path
        remove_end_lines(file_path)

In case you have subdirectory and want to perform the same operation you should use os.walk()

def FormatTXT(my_dir):
    for dir_path, dir_name, files_name in os.walk(my_dir):
    # files_name is a list of all file in dir_path,
        if files_name:  # if there is file in the current dir (the list is not empty)

        for file_ in files_names:
            file_path = os.path.join(my_dir, file_)  
            remove_end_lines(file_path)

I hope this help. if you have more question don't hesitate to ask

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.