I am trying to iteratively open some files to do some processing with the data. However, I haven't been able to make it work. I don't know what could be causing this.
sd = os.path.dirname(os.path.abspath(__file__))
file_names = []
for root,d_names,f_names in os.walk(os.path.join(sd, path)):
for f in f_names:
if f.endswith('.csv'):
file_names.append(os.path.join(root, f))
for f_name in file_names:
with open(f_name, 'r') as file:
...
I have also tried the following aproach, using pathlib
input_path = pathlib.Path(path)
file_names = input_path.glob('**/*.csv')
for f_name in file_names:
with open(f_name.resolve(), 'r') as file:
...
Both methods yield the same result.
'path' is the name of a directory that sits on the same directory as the script. Reading the error seems to indicate the path is correct. The files sit in a somewhat complex file structure with pretty long filenames at times.
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\...
To give a bit more insight, here is a brief simplified representation of the file structure of path
path
¦-dir1
¦¦-dir2
¦¦¦-dir3
¦¦¦¦-sub1
¦¦¦¦¦-file-1a
¦¦¦¦-sub2
¦¦¦¦¦-file-1b
¦¦¦¦¦-file-2b
What I've found by testing is that when I replace path by dir3 to remove uneccessary traversal, the script will process file-1a which is the only one in that directory and file-1b, but give the same error when reaching file-2b. Furthermore, when making sub2 the target instead, it will process all files inside sub2 with no issues.
Also, as suggested, I tried adding the line print(os.access(f_name, os.R_OK), repr(f_name)) just before attempting to open the file. It turns out it returns False every time just before the error is raised(followed by the file path), and returns True whenever I've managed to process a file.