Python读取文件空标记

2024-04-24 03:05:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在看泽德的旅游指南时遇到了问题。你知道吗

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 want that, hit RETURN"

raw_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 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write this to file."
target.write("%s \n %s \n %s \n" % (line1,line2,line3))

print target.read()

print "And finally, we close it"
target.close()

在r向我的文件中添加新内容(r+处于打开状态)之后,除了新添加的内容之外,我在记事本中还可以看到将近4000个空字符。这使得我的文件只是一个空白页上的提示。重点在哪里?你知道吗


Tags: toyoutargetinputrawifthatline