Python 3.4 - Opening a file in a module with a different directory -
i have package looks following
package/ __init__.py module.py
in module.py
have this
def function(file_name): open(file_name) f: # stuff
somewhere else in arbitrary directory have python file looks this
import package package.function("some_file.txt")
but upon running it, it's giving me filenotfounderror: [errno 2] no such file or directory: "some_file.txt"
.
the problem absolute path of some_file.txt
may c:\users\user\documents\some_file.txt
, in package.function
path absolute path c:\users\user\documents\package\some_file.txt
. there way can make calling package.function
file outside package
directory automatically includes absolute path of file want open?
sorry if terminology ambiguous, i'm unfamiliar os
stuff.
edit: exact file setup have looks this:
directory/ foo.py package/ __init__.py module.py another_directory/ bar.txt
and foo.py
looks this
import package package.function("another_directory/bar.txt")
i think you're missing point.
it's not important source code lies, relative paths (and pure file names are relative paths) interpreted relative directory program process runs in (ie. directory when type python c:\path\to\my\python\code\code.py
)
Comments
Post a Comment