Specified CSV file not found when running Python script

Asked 2 years ago, Updated 2 years ago, 56 views

I'm a Python beginner.
I am using the pandas library in the Python 3.8.8 environment of VScode, and I am trying to read the csv file in read_csv, but I get a FileNotFoundError.
The py and test.csv files in the image are in the same directory.
I would appreciate it if you could tell me how to deal with it.

src=

python vscode

2022-09-30 18:07

1 Answers

Perhaps the directory where Python is started is different from the directory where the script files are located.If the script file and the CSV file are in the same directory, you might want to do the following:

importos
import pandas aspd

csv_path=os.path.join(os.path.dirname(__file__), 'test.csv')
df = pd.read_csv(csv_path)


2022-09-30 18:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.