No such file or directory is displayed in python.

Asked 2 years ago, Updated 2 years ago, 80 views

I'm a python or programming beginner.I look forward to hearing from you.After writing the following code in atom, I saved it in my folder on my desktop as csvreader.py.

import csv
f=open("csv_example.csv", "r")
reader=csv.reader(f)
For row in reader:
    print(row)
f.close()

If you save it like this and start it with Anaconda Prompt,

(base) C: UUsers conconcrete-2>pythonC: UUsers conconcrete-2 esDesktop ppython ¥csvreader.py
Traceback (most recent call last):
File "C: UUsers conconcrete-2 DDesktop ppython "csvreader.py", line 3, in <module>
f=open("csv_example.csv", "r")
FileNotFoundError: [Error 2] No such file or directory: 'csv_example.csv'

It always comes out.
Please tell me what this error means and how to deal with it.

python csv

2022-09-30 16:51

1 Answers

FileNotFoundError: [Error 2] No such file or directory: 'csv_example.csv'

As written in English, the error csv_example.csv file or directory cannot be found.

The basic method of running the python program is

 python program name.py

Yes, if you specify a relative path in the open function, it means the relative path from the current directory when you execute this execution command.

For example, suppose the directory you are currently in is /home/user/Desktop/python.Then run csvreader.py in /home/user/Desktop/python/test1.

 python test 1/csvreader.py

At this time, csv_example.csv should be in /home/user/Desktop/python, so if it is in /home/user/Desktop/python/test1, the file is not found.

To avoid this error, you must place csv_example.csv in the current directory where you are running the python command.

Alternatively, you can specify the absolute path of csv_example.csv.


2022-09-30 16:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.