python - Loading data via a module and a text file in another directory -


i have directory structure:

main/ __init__.py      foo/          __init__.py          names.py          names.pickle      bar/          __init__.py          my_module.py 

names.py has code works names.pickle, including loading pickled data.

however, in my_module.py if do:

from main.foo import names 

then run my_module.py main/bar, python complains me can't find names.pickle, presumably because looks inside main/bar/, not main/foo/.

what recommended way resolve this? temporarily change os.curdir?

you should use file location of names.py locate names.pickle

from main.foo import names import os  names_pickle = os.path.join(os.path.dirname(names.__file__), 'names.pickle') 

each module has __file__ attribute tells located on file system.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -