python文件执行路径问题 10
python往相对路径中写日志,单独运行该问题,没问题,但是通过其他程序运行该python文件就会报错写日志的py文件:defwriteFileName(self,log...
python往相对路径中写日志,单独运行该问题,没问题,但是通过其他程序运行该python文件就会报错
写日志的py文件:
def writeFileName(self,logtext):
nowTime =str(time.strftime("%Y%m%d",time.localtime(time.time())))
timeForWrite = str(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))
fileName = "..\log\\result.txt"
if os.path.exists(fileName):
f = open(fileName,'a+')
f.write(logtext.decode("utf-8").encode("GBK"))
f.close()
else:
f = open(fileName,'w')
f.write(logtext.decode("utf-8").encode("GBK"))
f.close()
通过命令行运行报错:
C:\Users\wanglongjun>python D:\software\python\PyProject\robotization\pythonFile
\runfile\test1.py
Traceback (most recent call last):
File "D:\software\python\PyProject\robotization\pythonFile\runfile\test1.py",
line 21, in <module>
write.writeFileName("test2,2016-08-10 17:29:19,failse,time is out"+"\n")
File "D:\software\python\PyProject\robotization\pythonFile\runfile\test1.py",
line 16, in writeFileName
f = open(fileName,'w')
IOError: [Errno 2] No such file or directory: '..\\log\\result.txt'
在其他python文件中执行该文件也报错:
File "./runfile/test1.py", line 21, in <module>
write.writeFileName("test2,2016-08-10 17:29:19,failse,time is out"+"\n")
File "./runfile/test1.py", line 16, in writeFileName
f = open(fileName,'w')
IOError: [Errno 2] No such file or directory: '..\\log\\result.txt'
跪求各位大神帮忙看看需要如何解决这种情况,跪谢 展开
写日志的py文件:
def writeFileName(self,logtext):
nowTime =str(time.strftime("%Y%m%d",time.localtime(time.time())))
timeForWrite = str(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))
fileName = "..\log\\result.txt"
if os.path.exists(fileName):
f = open(fileName,'a+')
f.write(logtext.decode("utf-8").encode("GBK"))
f.close()
else:
f = open(fileName,'w')
f.write(logtext.decode("utf-8").encode("GBK"))
f.close()
通过命令行运行报错:
C:\Users\wanglongjun>python D:\software\python\PyProject\robotization\pythonFile
\runfile\test1.py
Traceback (most recent call last):
File "D:\software\python\PyProject\robotization\pythonFile\runfile\test1.py",
line 21, in <module>
write.writeFileName("test2,2016-08-10 17:29:19,failse,time is out"+"\n")
File "D:\software\python\PyProject\robotization\pythonFile\runfile\test1.py",
line 16, in writeFileName
f = open(fileName,'w')
IOError: [Errno 2] No such file or directory: '..\\log\\result.txt'
在其他python文件中执行该文件也报错:
File "./runfile/test1.py", line 21, in <module>
write.writeFileName("test2,2016-08-10 17:29:19,failse,time is out"+"\n")
File "./runfile/test1.py", line 16, in writeFileName
f = open(fileName,'w')
IOError: [Errno 2] No such file or directory: '..\\log\\result.txt'
跪求各位大神帮忙看看需要如何解决这种情况,跪谢 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询