2

I am looking to read a csv file present in my local drive ex: C:\Users\Studyfolder\abc.csv My python libraries are installed in another directory - a path created for python 3 libraries ex:C:\Users\Anaconda3_2\envs\py3

On Anaconda Prompt- i have set my cd path as - C:\Users\Anaconda3_2\envs\py3 since naturally, all python libraries will be installed there

On Jupyter Notebook, I am looking to read the csv file to extract the dataframe. For definite reason when i run the command df = pd.read_csv('abc.csv'), the file wouldn't be found under the path cd'd on Anaconda prompt

Should i be saving all my data files in the same path where python libraries are installed OR there is a better way i can still read the file without having to save it in the cd path shown above?

P.S New to Jupyter notebooks and Python in general

import pandas as pd

df = pd.read_csv('abc.csv')
df.head()

FileNotFoundError Traceback (most recent call last) in 1 # load abc data into data frame ----> 2 df = pd.read_csv('abc.csv')

1
  • 2
    Use the full pathname in the read_csv call. pd.read_csv(r'C:\Users\Studyfolder\abc.csv') Commented Jun 7, 2019 at 21:23

2 Answers 2

3

If you activate your anaconda environment, the jupyter environment should be tied to that interpreter. In that case, it doesn't really matter where you start your notebook from, it will always have access to the libraries installed there. For example:

conda activate py3

This will now tie conda to that environment:

import sys
sys.path
['C:\\Users\\Anaconda3_2\\envs\\py3'...]

So you can start jupyter wherever, as long as you pass a legitimate path. The full path will work anywhere:

# I'm at C:\Users\Anaconda3

df = pd.read_csv("C:\\Users\\Studyfolder\\abc.csv")

If you want to use relative paths, it's very dependent on where you call jupyter notebook from:

# Still at C:\\Users\\Anaconda3
df = pd.read_csv("..\\Studyfolder\\abc.csv")

Where the .. indicates to go back a directory

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

1 Comment

Thanks @C.Nivs - The relative path works great since i had activated the env py3
0

In general, I would suggest writing your scripts outside the python library in a directory structure that works best for your projects. The simple reason is that your python libraries are already added to PYTHONPATH and hence are available anywhere, whereas your project files are only available from your project folders.

In your case, there are two approaches you can take -

  • Provide the file location as a relative path to your current directory. So, df = pd.read_csv('..\..\..\Studyfolder\abc.csv')
  • Add the folder path into your system path by adding these statements before reading your file -
import sys
sys.path.append("C:\\Users\Studyfolder")

1 Comment

Thanks @Abhineed Gupta - I put the fullpath C:\\Users\\Studyfolder\abc.csv and it works totally fine..Which means i can run the Anaconda commands on the env that i had enabled in this case - C:\Users\Anaconda3_2\envs\py3, but i can still read the file from another directory by putting the relative path / full path as you stated

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.