I have a directory with the following subdirectories
|---Data
|
|---Notebooks
The Data
directory contains csv files, while the Notebooks
directory contains my Jupyter notebook files. How do I access the file from the Data
directory with a notebook at the Notebooks
directory?
My initial idea was this:
df = pd.read_csv('../Data/csvFile')
However the code block renders a file not found
error in Pandas.
The path you'll need will be relative to the directory you're running the Python process from normally, but you can use the following trick to make it relative to a file you're running instead:
from pathlib import Path
df = pd.read_csv(Path(__file__).parent/'Data'/'csvFile')
However the __file__
variable is not defined in Jupyter notebooks, so you'll have to use this hack instead to get the directory the notebook is in:
from pathlib import Path
df = pd.read_csv(Path(globals()['_dh'][0])/'Data'/'csvFile')
Note that this hack only works when running from Jupyter. Unfortunately I don't have a one-size-fits-all approach, unless you count detecting if you're in Jupyter, and then selecting which method you'll use based off of that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.