Running python codes from outside the directory when script has filepaths -
so have python script my_script.py inside folder test , outside folder. script requires access input data file my_data.dat stored inside folder test.
now, when try run script outside folder test, using simple python my_script.py, tries locate my_data.dat in folder running script , fails. how can tell use correct path without modifying path in my_script.py?
i don't want modify paths in script because script used generically various paths , whole thing automated later.
you need use absolute file path instead of relative file path.
to absolute path of directory containing python file:
import os root_dir = os.path.dirname(__file__) and can absolute path of my_data.dat
data_file_path = os.path.join(root_dir, 'my_data.dat') afterward use data_file_path instead of "my_data.dat"
Comments
Post a Comment