这是我的目录的可视化表示:导入模块:没有这样的文件或目录
这里是test1.py
....
def foo():
f=read("./test1.dat","r")
....
的代码片段这里是test2.py的代码
import imp
TEST1 = imp.load_source('test1', '../test1.py')
def test2():
TEST1.foo()
运行test2.py
cd subdir
python test2.py
得到IO错误:没有这样的文件或目录:“./test1.dat”
我的问题是:
,如果我不改变目录的结构,例如移动TEST2 .py到它的父目录,是否有可能使模块test1在模块test2中调用时找到正确的文件?
http://stackoverflow.com/questions/779495/python-access-data-in-package-subdirectory – 2013-03-20 18:07:42
@redShadow,是的,我只可以读取父目录。 – camino 2013-03-20 18:10:02