Python walking through directory

245 views Asked by At

I'm trying to open many text files into a specific directory in order to process the text with NLP functions. My code is:

path= r'c:\my\directory'    

for root, dirs, files in os.walk(path):
    for filename in files:
        if filename.endswith(".txt"):
            absolute_filename = os.path.join(root, filename)
            with open(absolute_filename, 'r') as f:
                content= f.read()
                ... processing code

The problem is that the above code open only the first 20 files, when it arrive to certan file it give me the following error:

IOError: [Errno 2] No such file or directory: 'C:\\my\\directory\\Andrej Aleksandrovic Mironov.txt'

I already wrote a question for the same problem and what I got is the code above. Can somebody help me?

1

There are 1 answers

0
CosimoCD On

Hey guys I solved the problem. I used the same code I posted but I changed the name of all files in my directory. I give them the same name and after I runned the code. It works, but I still don't understand why I couldn't open the files while they were keeping differents names.