易截截图软件、单文件、免安装、纯绿色、仅160KB

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
然后你的两个函数的逻辑


相关问答:

eric4怎么运行python程序?

python和pyqt以及Eric4都已经配置好了,打开Eric4写个python程序比如简单的
print“hello,world”,怎么编译运行了,Start菜单的所有命令都出现和图中差不多的对话框,应当怎么运行了?

求大家指教

没有人用 ......

python 中一个疑惑

return func 与return func()
有什么不同的?
return func返回的是一个函数对象,
return func()返回的是func()函数的返回结果。

楼上正解

一个是返回变量 一个是返回的时候 再调用一下func函数
我觉得是 ......

一个Python小脚本运行超慢

我用Python写了一个小脚本,作用是统计我e:\download目录下文件都包含了哪些扩展名,并打印出来,但是不知道为什么,这个脚本虽然可以正常的运行,但是运行的非常非常慢,平均每一次运行的时间都超过了1分钟才有结果 ......

Python 中如何写if

python

content=str.find('ok')  &&  content=str.find('not ok')

这样表示 在内容中找到 ok  或者 找到 not ok 对吗?
并列条件用 and 而不是&& ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号