python写文件时出有关问题
python写文件时出问题
异常代码如下:
write a file error: [Errno 2] No such file or directory: 'C:\x0cile.txt'
Traceback (most recent call last):
File "C:\Documents and Settings\USER\桌面\复件 test.py", line 84, in <module>
test()
File "C:\Documents and Settings\USER\桌面\复件 test.py", line 78, in test
writefile(s)
File "C:\Documents and Settings\USER\桌面\复件 test.py", line 59, in writefile
f.write(str(string))
UnboundLocalError: local variable 'f' referenced before assignment
实在不知道哪里错了,请大侠们帮忙看看,感激不尽
------解决方案--------------------
转义成'C:\x0cile.txt' 不能当文件名
strfile="C:\file.txt" 改r"C:\file.txt"或"C:\\file.txt"或"C:/file.txt"
异常代码如下:
write a file error: [Errno 2] No such file or directory: 'C:\x0cile.txt'
Traceback (most recent call last):
File "C:\Documents and Settings\USER\桌面\复件 test.py", line 84, in <module>
test()
File "C:\Documents and Settings\USER\桌面\复件 test.py", line 78, in test
writefile(s)
File "C:\Documents and Settings\USER\桌面\复件 test.py", line 59, in writefile
f.write(str(string))
UnboundLocalError: local variable 'f' referenced before assignment
- Python code
。。。。。。。。。。。 def writefile(string): strfile="C:\file.txt" try: f = open(strfile,'a') except IOError,e: print "write a file error:",e f.write(str(string)) f.close() def test(): fname = 'C:\ZLLKDoc.cpp' #raw_input('Enter filename: ') try: fobj = open(fname, 'r') except IOError, e: print "*** file open error:", e strFile=fobj.read() fobj.close() s = eraser(strFile) print s
实在不知道哪里错了,请大侠们帮忙看看,感激不尽
------解决方案--------------------
转义成'C:\x0cile.txt' 不能当文件名
strfile="C:\file.txt" 改r"C:\file.txt"或"C:\\file.txt"或"C:/file.txt"