2

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.

1 Answer 1

5

You need to flush the content using f.flush(). It writes the current content in the buffer to the file,

In [17]: f.flush.__doc__ Out[17]: 'flush() -> None. Flush the internal I/O buffer.' 

use the with statement for handling files, as it automatically closes the files for you:

with open("file.txt","w") as f: while(operator): f.write("string") f.flush() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.