Save to Text File from Infinite While Loop

If you're exiting the program abnormally, then you should expect that sometimes the file won't be closed properly.

Opening and closing the file after each write won't do it, since there's still a chance that you'll interrupt the program while the file is open.

The equivalent of the CTRL-C method of exiting the program is low-level. It's like, "Get out now, there's a fire, save yourself" and the program leaves itself hanging.

If you want a clean close to your file, then put the interrupt statement in your code. That way you can handle the close gracefully.


Use a with statement -- it will make sure that the file automatically closes!

with open("file.txt", "w") as myFile:
    myFile.write(DATA)

Essentially, what the with statement will do in this case is this:

try:
    myFile = open("file.txt", "w") 
    do_stuff()

finally:
    myFile.close()

assuring you that the file will be closed, and that the information written to the file will be saved.

More information about the with statement can be found here: PEP 343


A file's write() method doesn't necessarily write the data to disk. You have to call the flush() method to ensure this happens...

file = open("file.txt", "w")
while True:
    file.write( DATA )
    file.flush()

Don't worry about the reference to os.fsync() - the OS will pretend the data has been written to disk even if it actually hasn't.