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
然后你的两个函数的逻辑
相关问答:
在list中添加一个类的局部变量 这样做是否合法 请看下面例子:
Python code:
class A():
def __init__( self ):
self.__a = 0
self.__b = 'hello'
def get_a( self ):
ret ......
在文本文件中匹配项包含中文
如内容为:gamename=中文
key=天下
文本文件的编码的文件为utf-8
python代码如下:
# -*- coding:UTF-8 -*-
contents=open(from_pa ......
菜手请教诸位老鸟:
我希望编辑zip压缩包里的一个文本文件。怎么解决比较合理?
我的想法:先把ZIP解压,再编辑文本,然后再压缩成ZIP并把解压过的文件删除。不过感觉有些繁琐,可不可以直接读ZIP然后修改呢? ......
C#里面用using System.Security.Cryptography; RSACryptoServiceProvider加密过的消息
用Python Crypto中RSA的方法 能直接 解吗?
RSA加密方式不是统一的吗?需要注意些什么?
有帮助。但是没解决问题。。。
......