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
。。。。。。。。。。。 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