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
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
实在不知道哪里错了,请大侠们帮忙看看,感激不尽
test() 函数中第一行加入 global f
然后你的两个函数的逻辑
相关问答:
请问下,我的是python2.5安装了PIL模块,然后编写程序打开本机上的一个图片时,为什么老出现错误呢,老是找不到图片,错误提示如下:File "C:\Python25\Lib\site-packages\PIL\Image.py", line 1888, in o ......
>>> x=-1
>>> if x>0:
print ('11')
>>> else:
SyntaxError: invalid syntax (<pyshell#20>, line 1)
网上搜的也是:
>>> x=int(input("Please ent ......
我用Python写了一个小脚本,作用是统计我e:\download目录下文件都包含了哪些扩展名,并打印出来,但是不知道为什么,这个脚本虽然可以正常的运行,但是运行的非常非常慢,平均每一次运行的时间都超过了1分钟才有结果 ......
python
content=str.find('ok') && content=str.find('not ok')
这样表示 在内容中找到 ok 或者 找到 not ok 对吗?
并列条件用 and 而不是&&
......