返回信息流from sys import argv
script,filename=argv
print "We're going to erase %r."%filename
print "If you don't want that,hit CTRL-C(^C)."
print "If you do want that,hit RETURN."
raw_input("?")
print"Opening the file..."
target=open(filename,'w+')#这里r+,w+的区别是什么呢?如果只写了w,下面的read()也会报错吗?
print "Truncating the file.Goodbye!"
target.truncate()
print "Now I'm going to ask you for three lines."
line1=raw_input("line 1:")
line2=raw_input("line 2:")
line3=raw_input("line 3:")
print "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
#下面三行总是报错TypeError:coercing to Unicode:need string or buffer,file found
#如果直接写成target.read()也会报错:IOError:[Error 0] Error
txt=open(target)
print txt.readline()
print txt.read()
print "And finally,we close it."
target.close()
这是一条镜像帖。来源:北邮人论坛 / python / #6249同步于 2015/4/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
初学者提问,I/O报错
sunny7U
2015/4/12镜像同步5 回复
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
【 在 sunny7U 的大作中提到: 】
: from sys import argv
: script,filename=argv
: print "We're going to erase %r."%filename
: ...................
target是文件,不是文件名,所以target不能作为open的参数。
r,w,r+,w+,rb,wb什么的,看文档好了。
另外,如果没有特殊原因的话,学Python3吧。
python 3无法用wxpython呀,暖神有推荐的GUI工具吗?
【 在 nuanyangyang 的大作中提到: 】
:
: target是文件,不是文件名,所以target不能作为open的参数。
: r,w,r+,w+,rb,wb什么的,看文档好了。
: ...................
【 在 dcy0701 的大作中提到: 】
: python 3无法用wxpython呀,暖神有推荐的GUI工具吗?
pyqt4, pyqt5都支持Python3。PyQt4的文档好一些,但Qt5是新版本。
=-= 你不会是被 wx “跨平台” 给安利了吧,大概还是几百年前中文资料,还是说你要 MFC style。。
【 在 dcy0701 的大作中提到: 】
: python 3无法用wxpython呀,暖神有推荐的GUI工具吗?
# target只是一个类似于文件句柄的概念
target.flush()
target.close()
with open(filename, 'r') as fh:
print fh.read()
【 在 sunny7U 的大作中提到: 】
#下面三行总是报错TypeError:coercing to Unicode:need string or buffer,file found
#如果直接写成target.read()也会报错:IOError:[Error 0] Error
txt=open(target)
print txt.readline()
print txt.read()
print "And finally,we close it."
target.close()