易截截图软件、单文件、免安装、纯绿色、仅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
然后你的两个函数的逻辑


相关问答:

[提问]Python 如何忽略 whitespace 读入

像 C 的 scanf() 那样
比如读入 1 2 3 a b c
每次读入一个
下面这个应该是你想要的吧:
Python code:

>>> k = raw_input()
0 0123 ds dsl sd
>>> k
'0 0123 ds dsl sd'
>>> ......

python怎么print 'hello world'

大家好,我是一个新手,刚开始学python,但是刚开始的helloworld都没法打印,让我很无奈。
我的python安装路径为f:\python31。在path中也设置对了,在windows下运行是这样显示的,希望各位前辈指点一下。谢谢
Pyth ......

python 正则匹配中文

在文本文件中匹配项包含中文
如内容为:gamename=中文 
  key=天下
文本文件的编码的文件为utf-8
python代码如下:
# -*- coding:UTF-8 -*-
  contents=open(from_pa ......

python urllib urllib2 httplib 访问url的问题

我想用python解析网页中的url 在下载之前, 我想先发一个head请求 不需要下载整个网页, 先获得该网站的head信息, 读取一些信息之后 在调用这些库的read方法来读取整个网页 该怎么操作呢 多谢
帮顶了

有人能指教一 ......

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号