So I wrote a simple script that writes some lines to a file:
f = open('file.txt','w')
while(operator):
f.write("string")
f.close()
The problem is that while the script is running the file remains empty, only when the script finishes and closes the file, the contents become visible. What is happening and how can I make it so that what the script writes to the file is immediately visible when the script is running?
I’m running BackTrack 5 to run the script.
You need to flush the content using
f.flush().It writes the current content in the buffer to the file,
use the
withstatement for handling files, as it automatically closes the files for you: