3

Im trying to open a csv file as followed

My ipynb-file is in the following directory -> /data/filename.ipynb where as my csv file is in the following directory -> /data/preprocessed/processed_data.csv

When i try to open the file with the following code:

df = pd.read_csv('/preprocessed/processed_data.csv')

I get an exception that the file doesn't exist. Somehow I think I didn't quite understand how the directories work. Please help, thanks.

1
  • Remove the first /? Commented Jan 13, 2022 at 11:56

2 Answers 2

2

try this-

df = pd.read_csv('preprocessed/processed_data.csv')
Sign up to request clarification or add additional context in comments.

1 Comment

oh yeah i had one slash too much thanks
0

Try using the os module with a relative path:

import os

filename = os.path.join(os.path.dirname(__file__),'path/to/file/processed_data.csv')
df = pd.read_csv(filename)

Comments

Your Answer

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