出现了两个不明白的地方:
open()的参数,W是write,A是add,r是read,就和其他语言一样
然后是truncate
网上说是截断的意思,但是我这里把这行删去之后没有任何异常出现
filename = raw_input("Which document you want to open?")
txt =open(filename)
print "We're going to erase %r." %filename
print "If you don't want that, hit CTRL-C to exit"
print "If you do want that, hit RETURN"
raw_input("?")
target = open(filename,'w') # w for write, in this mode we are allowed to refresh the text instead of READONLY
target.truncate()
line1 = raw_input("line 1:")
target.write(line1)
target.close()
顺便说一句
python的文件IO比C好用一万倍
更新:truncate是清空文档的意思,如果传递了w参数给open()函数则truncate无意义。如果传递a参数才能看到结果