【发布时间】:2014-07-05 12:45:55
【问题描述】:
这是 python(3.4) 代码:
test = open('test.txt', 'r+')
test.truncate();
i = 0
stop = 99
while i <= stop:
test.write("{:0>{}}|".format(i, len(str(stop))))
i += 1
print(test.read())
它可以很好地写入文件,但由于某种原因它不会打印它。
test = open('test.txt', 'r+')
print(test.read())
这会按预期打印,所以我不知道问题可能出在哪里。
更新:
使用 seek(0) 解决了它。你能链接一个关于它的解释吗?我在该语言的文档中找不到它。
【问题讨论】:
-
写入文件后,文件指针位于文件末尾,因此
read()没有任何意义。可能相关:stackoverflow.com/questions/14271216/… -
您要查找的文档是
io模块的文档。具体来说,类文件对象继承自IOBase; here is the docs on seek
标签: python python-3.x printing windows-7