os.listdir returns the error: no such file or directory even though the directory actually exists

Question:

This is my project files structure:

hierarchy

and this is my main.py script:

if __name__ == '__main__':
    leukemia_dir = "../dataset/leukemia"  # if I click here, I get redirected to the folder
    file_names = os.listdir(leukemia_dir)  # << won't work

Unfortunately, os.listdir(leukemia_dir) returns me the following error:

FileNotFoundError: [Errno 2] No such file or directory: ‘../dataset/leukemia’

If I remove ../ from leukemia_dir, it works. Furthermore, os.getcwd() returns /Users/John/Desktop/Leukemia.

What am I missing?

Asked By: tail

||

Answers:

The issue is that you’re confused about the relative path. ../dataset/leukemia is relative to the current working directory of the executing shell, not the file which is being run.

This means that if you run python src/main.py from /Users/John/Desktop/Leukemia, the CWD will is /Users/John/Desktop/Leukemia, and thus ../dataset/leukemia resolves to /Users/John/Desktop/dataset/leukemia.

Simply using dataset/leukemia works because it is the relative path from the project root. It resolves to the absolute path /Users/John/Desktop/Leukemia/dataset/lukemia. Generally, you can safely assume your code is being run from the project root, so you won’t need to use ...

Answered By: Michael M.

Instead of relying on the CWD and how the script is executed, it may be safer to rely on the directory structure and the relative location of the dataset compared to the main.py file:

if __name__ == '__main__':
    leukemia_dir = os.path.join(os.path.dirname(__file__), '..', 'dataset', 'leukemia')
    file_names = os.listdir(leukemia_dir)  
Answered By: Mureinik
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.