【发布时间】:2014-09-18 08:15:56
【问题描述】:
运行这个:
import os
if __name__ == '__main__':
exclude = os.path.join(
r"C:\Dropbox\eclipse_workspaces\python\sync\.git", "info", "exclude")
with open(exclude, 'w+') as excl: # 'w' will truncate
# print excl.read() # empty
# excl.readall() # AttributeError: 'file' object has no attribute
# 'readall' -- this also I do not understand
excl.write('This will be written as expected if I comment the
line below')
print "Garbage\n\n", excl.read()
# if I do not comment the line however, the file contains all the garbage
# excl.read() just printed (edit: in addition to the line I wrote)
导致我的文件充满垃圾 - 为什么?还有为什么 readall 没有解决?
Python 2.7.3
最新迭代:
#!/usr/bin/env python2
import os
if __name__ == '__main__':
exclude = os.path.join(r"C:\Users\MrD","exclude")
with open(exclude,'w+') as excl:
excl.write('This will be written if I comment the line below')
print "Garbage\n\n",excl.read()
# now the file contains all the garbage
raw_input('Lol >')
【问题讨论】:
-
为什么会有
.readall()方法? -
你能告诉我们你看到的“垃圾”吗?因为您的文件指针位于文件末尾,而
excl.read()在该点返回一个空字符串。 -
@MartijnPieters:Pycharm 解决了它
-
不知道 PyCharm 是从哪里得到的,但 Python file objects 上没有这种方法。
-
对,PyCharm 与 Python 3
io.RawIOBasetype 混淆了。
标签: python python-2.7 file-io io