我有以下代码:
#!/usr/bin/python
import os
from sys import exit as EXIT
File = os.path.realpath(__file__)
dir = os.path.dirname(File)
os.chdir(dir)
path_file = os.path.join(dir,"path.txt")
filo = open(path_file)
lines = filo.readlines()
if len(lines) == 1:
path = lines[0]
else:
raw_input("Too many lines... ")
EXIT()
FILE = open(path)
当我运行它时,我得到错误:
Traceback (most recent call last):
File "py/Zebra/zebra.py", line 20, in <module>
FILE = open(path)
IOError: [Errno 2] No such file or directory: 'C:\\Users\\user\\file.txt'
如果路径是硬编码的,那么我可以执行类似 path = r"C:\path\to\file" 的操作
但由于该变量是由其他方法定义的,我现在不确定要解决这个问题:(