Home > other >  Receiving "no such file or directory" error within a for loop - recognizes file initially
Receiving "no such file or directory" error within a for loop - recognizes file initially

Time:09-30

Code below:

for WorkingFile in os.listdir(path):  
    print(WorkingFile)
    xlsx = pd.ExcelFile(WorkingFile)

returns this:

ChetworthPark_Test.xlsx
FileNotFoundError: [Errno 2] No such file or directory: 'ChetworthPark_Test.xlsx'

So it's printing the file name (demonstrating that it recognizes the path), but then not passing it to the variable "xlsx" after. Any ideas on where I'm going wrong? For more context, I'm running this in Google Colab.

CodePudding user response:

os.listdir returns the file name not the path. So you need to prepend the path:

for fn in os.listdir(path):
   do_something(f"{path}/{fn}")

As pointed out in a comment, / for paths is not universal, so we have os.path.join:

from os.path import join
for fn in os.listdir(path):
    fn = join(path, fn) # handles / or \
    do_something(fn)

However for a fair while now we've had pathlib.Path which makes this much easier:

from pathlib import Path
for fn in os.listdir(path):
    do_something(Path(path) / fn)

or, more naturally with pathlib:

from pathlib import Path
for fn in Path("/path/to/look/at").expanduser().resolve().glob("*"):
    if not fn.is_file():
        continue
    do_something(fn)

(note that I've also handled expanding things like ~/some-file and simplifying the path here)

  • Related