Cannot open file found by os.walk (python 3.7)

56 views Asked by At

I am using os.walk to recursively search through a directory to find a file and parse it for data. Although os.walk is able to find the file, trying to open it yields a FileNotFoundError. It can find and open other files in the same directory however.

import os
import re

for root, subdirs, files in os.walk(r'main_dir'):
    for filename in files:
        try:
            if re.search('aimless', filename):
                with open(os.path.join(root, filename)): as f:
                    for line in f:
                        #do stuff
                print('Read file: ' + filename)

            elif re.search('pdb', filename):
                print('Trying to find file: ' + os.path.join(root, filename))
                #open file with full path
                with open(os.path.join(root, filename)) as f:
                    for line in f:
                        #do stuff
                print('Read file: ' + filename)
        except FileNotFoundError:
            print('Could not find file: ' + str(filename))
            print('Changing directory...')
            try:
                #change directory to file's directory to remove need for full path
                os.chdir(root)
                print('Found root: ' + root)
            except FileNotFoundError:
                print('Could not find root: ' + root)
            try:
                #open file without full path
                with open(filename) as f:
                    for line in f:
                        #do stuff
            except FileNotFoundError:
                print('Still could not find file: ' + filename)
                print(str(os.listdir()) + '\n')      


You will notice in the output that it displays the file in the directory, however even after changing to the directory so that I don't have to worry about providing full file path, it still doesn't find the file when it tries to open it.

Output:

Read log file: file1_aimless.log
Trying to find file: main_dir/file2.pdb
Could not find file: file2.pdb
Changing directory...
Found root: main_dir
Still could not find file: file2.pdb
['file1_aimless.log', 'file2.pdb']

I know windows uses backslash as file separator, though I don't understand why it's able to find one file with backslashes in the file path but not another from the same directory. I've tried doing this with and without 'r' in front of the directory, receiving the same issue each time. This isn't my exact code but it should describe what I'm trying to do. Why won't it open a file it just found?

(Also, pdb file is a protein data bank file, however the issue isn't isolated to just this type of file - it happens with seemingly any type of file)

0

There are 0 answers