This is a bit strange. My program isn’t able to write the output to a file. Its even not showing any error. I simultaneously used standard output and found that its showing..
This is my code: (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))
Can anyone tell me why this is not writing to the file.
I tried to use "w+", "w" modes and also writelines() method but they didn’t work.
Edited
However, it worked when I closed the file using op.close()
- Why this is happening? (I didn’t encounter it previously)
- Does python immediately write to the file as soon as it finds
op.write()?
You need to close the file for it to write to disk. Add op.close() to the end of your code.