How to open a file in pandas

14,743

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.

Share:
14,743
iFunction
Author by

iFunction

Updated on June 17, 2022

Comments

  • iFunction
    iFunction almost 2 years

    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.