Reading file from a local folder python after being imported from a parent folder -
there file main.py
in folder a. folder b subfolder of folder a. there files subfolder.py
, data.txt
inside folder b. subfolder.py
has function reads data.txt
. reading happens when run file subfolder.py
now in main.py
import subfolder.py
, call function reads data.txt
says "no such file or directory data.txt
"
don't understand do. can help?
could please show function in subfolder.py
.
the program may unsure of data.txt file is. run in first file:
import os print os.path.dirname(os.path.realpath(__file__))
then add on string needed
if python behaves in way expect behave, may need run
import os filepath = os.path.dirname(os.path.realpath(__file__))+"\\"+"subfolder"+"\\"+"data.txt"
and use filepath in function in subfolder.py open data.txt.
does help?
Comments
Post a Comment