简体   繁体   中英

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:

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.

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 !')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM