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

python: How to read file independent from entry point (both notebook and .py files)

I’m working on a python project that looks like this

app
├── data
│   └── my_data.csv
├── utils
│   └── reader.py
├── scripts
│   └── s0.py
├── notebooks
│   └── n0.ipynb
└── main.py

In reader.py:

def read_file():
    with open('./data/my_data.csv'):
        print('I can read the file !')

I test.py, main.py and n0.ipynb files:

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

from utils.reader import read_file
read_file()

To run s0 and main, I use the command: python -m scripts.s0 and python -m main and it works fine.

But when I try to run the notebook, it does not. I understnd why (it looks at ./data/my_data.csv but since it’s not at root level, it does not work)

Is there a way to make file reading independent from entry point in python ?

In javascript for instance, it would be esay to do, I would use path ../data/my_data.csv in reader file and it would work independently from the file location the function is called.

>Solution :

You can find the path of the current file with the __file__ variable (also see this answer).
From that, you can construct the required path. In reader.py:

from pathlib import Path

APPDIR = Path(__file__).parent.parent.resolve()

def read_file()
    with open(APPDIR / "data" / "my_data.csv"):
        print('I can read the file !')
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