这有点奇怪。我的程序无法将输出写入文件。它甚至没有显示任何错误。我同时使用标准输出,发现其表现..无法写入文件
这是我的代码:(Python 2.7版)
#!/usr/bin/env python
import re
def isRecycled(n,m):
n = str(n)
m = str(m)
try:
...........
...........
My Code.
except ValueError:
return False
ip = open("C-small-attempt0.in", "r")
op = open("output.txt", "a")
for num, eachLine in enumerate(ip.readlines()[1:]):
......
......
My code
## This is WORKING
print "Case #%d: %d" %(num+1, count)
## This is NOT Working
op.write("Case #%d: %d" %(num+1, count))
谁能告诉我这是为什么不写入文件。
我试图使用"w+"
,"w"
模式和writelines()
方法,但他们没有工作。
编辑
但是,它工作时,我关闭使用op.close()
- 为什么发生这种情况的文件吗? (我以前没有遇到过)
- python是否立即写入文件一旦发现
op.write()
?
呀,现在它的工作。 – Surya 2012-04-14 14:05:15
如果可能,你能告诉我为什么发生了。我写了许多小拼图,我没有关闭文件......并且他们工作。为什么在这种情况下发生? – Surya 2012-04-14 14:06:11
这是由于缓冲。请参阅file.write()方法的文档 - http://docs.python.org/library/stdtypes.html#file-objects – timc 2012-04-15 04:15:00