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.")
print("If you do want that,hit RETURN.")
input("?")
print("Opening the file......")
target = open(filename,'r+')
print("Truncating the file.Goodbye!")
target.truncate()
print("Now I'm going to ask you for three lines.")
line1 = input("line 1:")
line2 = input("line 2:")
line3 = input("line 3:")
print("I'm going to write these to the file.")
target.write("%s,\n%s,\n%s"%(line1,line2,line3))
print(target.read())
print("And finally,we close it.")
target.close()
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.")
print("If you do want that,hit RETURN.")
input("?")
print("Opening the file......")
target = open(filename,'r+')
print("Truncating the file.Goodbye!")
target.truncate()
print("Now I'm going to ask you for three lines.")
line1 = input("line 1:")
line2 = input("line 2:")
line3 = input("line 3:")
print("I'm going to write these to the file.")
target.write("%s,\n%s,\n%s"%(line1,line2,line3))
target.close()
target = open(filename,'r')
print(target.read())
print("And finally,we close it.")
target.close()
1)26行之前加上代码 print(target.tell()),检查当前文件指标位置 -- 此时应该在文件尾
2)26行之前加上代码 target.seek(0),让文件指针指向文件的0位置,即启始位置
然后再 print(target.read())
具体查看python 对 file处是的相关帮助