FileNotFoundError: [Errno 2] No such file or directory: 'statistics.csv'

2.1k Views Asked by At
txt = open("statistics.csv")

Keep getting this error, FileNotFoundError: [Errno 2] No such file or directory: 'statistics.csv', however the file is in the same folder as main.py

Projects:
  US_statistics:
    statistics.csv
    main.py

tried using path

C:\Users\user_name\OneDrive\Documents\CODE\Programs\Python\Projects\US_statistics\statistics.csv

yet I still get the same error.

used os.getcwd and os.path.realpath(file) and paths match up to where the files are.

2

There are 2 best solutions below

1
On

Try to run it directly over

python main.py 

instead of in a environment or over the run button from Visual Studio Code this should work?

2
On

If you use Visual Studio Code You should note that you have opened the working environment exactly from inside the folder where the Python file is located