Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a file in pandas

Having difficulty opening a csv file in pandas, I have tried:

data = pd.read_csv("/home/me/Programming/data/sample.csv")

That did not work, so I tried:

import os
cwd = os.getcwd()

data = pd.read_csv(cwd + "sample.csv")

and that doesn't work either, just says that file does not exist, but it's there in the file manager clear as day.

like image 705
iFunction Avatar asked Sep 13 '25 18:09

iFunction


1 Answers

os.getcwd() return the current working directory without trailing path separtor.

You should use os.path.join instead of + to correctly join paths:

import os

cwd = os.getcwd()
data = pd.read_csv(os.path.join(cwd, 'sample.csv'))

BTW, there's no need to specify full path of current working directory; just specify sample.csv should be enough:

data = pd.read_csv("sample.csv")

Make sure the file sample.csv is in the current working directory.

like image 127
falsetru Avatar answered Sep 15 '25 07:09

falsetru