Home > database >  Error on creating relative path in Jupyter Notebook using read_csv
Error on creating relative path in Jupyter Notebook using read_csv

Time:10-31

I am a beginner with Jupyter Notebook but here's what I am having trouble with:

I have created a Python3 file using Jupyter Notebook and have imported my csv file using read_csv.

Then, I created a new folder (named Park )- that would include my python file, and the csv file needed (named MyFile.csv). The purpose is to be able to run the python file from any computer- not just my own- using this relative path.

So, I do:

  import pandas as pd
  data=pd.read_csv('Park/MyFile.csv')
  data.head()

And I get this error:

No such file or directory: 'Park/MyFile.csv'.

On the contrary, when I simply run this:

   import pandas as pd
   data=pd.read_csv('MyFile.csv')
   data.head()

It runs fine. But will this work (only including the file's name) when I try to run the Python file from other computers?

All I am trying to do is to be able to send out the folder (as zipped maybe?) and have it run smoothly in any computer using a relative path for the csv files.

Any suggestion would be appreciated.

Thanks!

CodePudding user response:

This problem comes from the relative path of your system. If you want to run another file under the same directory, you could use "./Myfile.csv".

The "./" stands for father directory of this file - that is the current directory, so it will search file under current directory.

If you input "Park/Myfile.csv", the interpreter will search for "Park" file under current "Park" directory and ends up none existing.

  • Related