0

I have a python script that loads data files using relative paths. I then have a shell script from a parent directory that runs the python script like so

automate.sh contents:

python3 /path/to/python/file/file.py 

file.py contents:

import numpy as np
np.loadtxt(./data/data2load.txt)

The problem is when I run this python script from the shell script, the relative path is broken. It tries to find the file to load in /path/to/shell/script/data/data2load.txt. Is there a robust way to fix this without using absolute paths?

The fix I currently have is in my automate.sh, I instead write:

cd /path/to/python/file
python3 file.py
cd ../../../.. 

But this is obviuosly really tedious

1 Answer 1

1

You should be able to do the following:

import os

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_PATH = os.path.join(BASE_DIR, "data/data2load.txt")

the value of DATA_PATH will now be /path/to/python/file/data/data2load.txt, so:

np.loadtxt(DATA_PATH)

should always load the correct file.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Just to confirm, is BASE_DIR set to the directory that' calling the python script? In this case it's the directory of the shell script?
Yes, __file__ is always set to the full path of the script itself therefore BASE_DIR will be the parent directory of the .py file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.