【发布时间】:2016-08-17 15:51:25
【问题描述】:
我有一个程序,它依赖于用户输入来输入文件,以便程序在 Python 2.7.11 中打开。我在原始目录Detector 中名为TestCases 的子目录中拥有所有这些文件,但是从超级目录运行程序时,我似乎无法访问TestCases 中的文件。我尝试使用os.path.join,但无济于事。这是我的代码:
import os.path
def __init__(self):
self.file = None
os.path.join('Detector', 'TestCases')
while self.file == None:
self.input = raw_input('What file to open? ')
try:
self.file = open(self.input, 'r')
except:
print "Can't find file."
我运行程序时的终端如下:
>>> What file to open? test.txt # From the TestCases directory
>>> Can't find file.
>>> What file to open? ...
我是否错误地使用了os.path.join?我认为它应该链接两个目录,以便在从超级目录运行程序时可以从子目录访问文件。
【问题讨论】:
-
您正试图在当前目录中打开文件,而不是在
TestCases目录中
标签: python python-2.7 file path directory