I'm getting 'FileNotFoundError: [Errno 2] No such file or directory' for a file in a subfolder. How do I fix this in VS Code?

Responsive Ad Header

Question

Grade: Education Subject: Support
I'm getting 'FileNotFoundError: [Errno 2] No such file or directory' for a file in a subfolder. How do I fix this in VS Code?
Asked by:
125 Viewed 125 Answers

Answer (125)

Best Answer
(282)
If your file is in a subfolder (e.g., `data/my_file.csv`), and your script is in the root folder, a common way to access it is `open('data/my_file.csv')` if your script is run from the root. Using `pathlib.Path(__file__).parent / 'data' / 'my_file.csv'` is a more reliable approach.