Need to read a csv file that is in a different path than the one where jupyter notebook is run on
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
Shalin
I try to relax my mind on tough days and when my mind is relaxed, I try to do my work, mind my business and learn things i don't know. Perfect recipe for trying to stay relevant in an otherwise fleeting age
Updated on June 04, 2022Comments
-
Shalin almost 2 years
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')
-
John Gordon almost 5 yearsUse the full pathname in the read_csv call.
pd.read_csv(r'C:\Users\Studyfolder\abc.csv')
-
-
Shalin almost 5 yearsThanks @C.Nivs - The relative path works great since i had activated the env py3
-
Shalin almost 5 yearsThanks @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