本问题已经有最佳答案,请猛点这里访问。
我有一个可以在本地读取文件的python程序,很好:
在我拥有这个程序的目录中,有一个名为path_list的文件(它是一个文件路径列表),我可以这样打开和访问它:
test_explicit = open('path_list').read()
print 'Reading local file gives: ' + test_explicit
然后程序将遍历这些路径,并在每个路径上调用以下函数,根据在上面的版本目录中找到的内容执行操作。不幸的是,在这里,当我使用绝对路径而不是相对路径时,这些相同的打开/读取操作会导致"没有此类文件或目录"错误。(但当我打印出它要去的地方并在那里时,我看到了我期望的内容)。
以下是我的代码的相关部分:
def getCommand(path):
# Grab that trailing /version, strip the v, convert to int
split_path = path.split("/")
version = split_path.pop()
version_num = int (version[1:] )
# Increment that number, and remake path with a fresh /v(x+1) suffix
version_num += 1
new_suffix = '/v' + str(version_num)
higher_file_path =