運行此:爲什麼在python中調用file.read填充垃圾文件?
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解決了這個問題 –