Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access the data within a subfolder, when my main script is also in a subfolder?

enter image description here

Hello, so I have a jupyter notebook in the data/processed folder and I want to access the json and csv file I have downloaded into the data/raw folder. I can’t seem to write the correct path for it.

data\
  processed\
    datacleaning.ipynb
  raw\
      getdata.ipynb
      EXAMPLE.json
      EXAMPLE.csv
src\
  results.ipynb
import pandas as pd
import json

raw_data = open("./data/raw/EXAMPLE.json")

i got the FileNotFoundError: [Errno 2] No such file or directory: ‘./data/raw/EXAMPLE.json’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Anyone know what the correct path would be?

>Solution :

Let’s assume your project lives in the absolute folder /home/karo/projects/.
Your notebooks current directiory is /home/karo/projects/data/processed.

When you write

raw_data = open("./data/raw/EXAMPLE.json")

it will be resolved to

raw_data = open("/home/karo/projects/data/processed/data/raw/EXAMPLE.json")

As you can see that’s not an existing path.

You can either write:

  • raw_data = open("/home/karo/projects/data/raw/EXAMPLE.json")
  • raw_data = open("../raw/EXAMPLE.json")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading