2

(Very new coder, first time here, apologies if there are errors in writing)

I have a csv file I made from Excel called SouthKoreaRoads.csv and I'm supposed to read that csv file using Pandas. Below is what I used:

import pandas as pd
import os

SouthKoreaRoads = pd.read_csv("SouthKoreaRoads.csv")

I get a FileNotFoundError, and I'm really new and unsure how to approach this. Could anyone help, give advice, or anything? Many thanks in advance

13
  • Is the file "SouthKoreaRoads.csv" in the same directory as the python file? If not you would need to update your path accordignly. Commented Jul 15, 2021 at 7:41
  • @AbdelrahmanShoman how can I check this? Commented Jul 15, 2021 at 7:49
  • In your code you are trying to read a file named "SouthKoreaRoads.csv". This file has to be somewhere on your computer and the same goes for your python/jupyter notebook. Are they both in the same directory (or folder) or not? ... If you are not able to tell if two files are in the same folder maybe you need some windows basics (assuming you are using windows) (youtube.com/watch?v=4xS5IOg_nDw) Commented Jul 15, 2021 at 7:53
  • Under Users > kahaa I have .conda, .ipython, .jupyter, anoconda3, and SouthKoreaRoads.csv Is that what you mean? Sorry probably a really beginer question Commented Jul 15, 2021 at 7:55
  • do you have the absolute path for the CSV file? And the absolute path for the python file? for example something like this: C:\Users\kahaa\SouthKoreaRoads.csv and C:\Users\kahaa\python_example.ipynb . If you are still not sure, maybe check this youtube.com/watch?v=-QAED9UaMIE Commented Jul 15, 2021 at 8:07

3 Answers 3

4

just some explanation aside. Before you can use pd.read_csv to import your data, you need to locate your data in your filesystem.

Asuming you use a jupyter notebook or pyton file and the csv-file is in the same directory you are currently working in, you just can use:

import pandas as pd SouthKoreaRoads_df = pd.read_csv('SouthKoreaRoads.csv')

If the file is located in another directy, you need to specify this directory. For example if the csv is in a subdirectry (in respect to the python / jupyter you are working on) you need to add the directories name. If its in folder "data" then add data in front of the file seperated with a "/"

import pandas as pd SouthKoreaRoads_df = pd.read_csv('data/SouthKoreaRoads.csv')

Pandas accepts every valid string path and URLs, thereby you could also give a full path.

import pandas as pd SouthKoreaRoads_df = pd.read_csv('C:\Users\Ron\Desktop\Clients.csv')

so until now no OS-package needed. Pandas read_csv can also pass OS-Path-like-Objects but the use of OS is only needed if you want specify a path in a variable before accessing it or if you do complex path handling, maybe because the code you are working on needs to run in a nother environment like a webapp where the path is relative and could change if deployed differently.

please see also:

https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html https://docs.python.org/3/library/os.path.html

BR

Sign up to request clarification or add additional context in comments.

Comments

0
SouthKoreaRoads = pd.read_csv("./SouthKoreaRoads.csv")

Try this and see whether it could help!

2 Comments

Thanks for the response mate. I tried the following and got a FileNotFoundError again. But, when I look, I see SouthKoreaRoads.csv, an internal shortcut, which I thought meant it existed.
What do you mean by an internal shortcut?
0

Try to put the full path, like "C:/users/....".

1 Comment

I'll try it. How exactly should I format it if its in the folders Users --> kahaa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.