Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save to Text File from Infinite While Loop

I am currently writing data from an infinite while loop to an SD Card on a raspberry pi.

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

It seems that sometimes file.txt doesn't always save if the program isn't closed through either a command or a keyboard interrupt. Is there a periodic way to save and make sure the data is being saved? I was considering using

open("file.txt", "a")

to append to file and periodically closing the txt file and opening it up again. WOuld there be a better way to safely store data while running through an infinite while loop?

like image 733
d.mc2 Avatar asked May 31 '13 18:05

d.mc2


3 Answers

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.

like image 89
Aya Avatar answered Oct 17 '22 07:10

Aya


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

like image 32
James Avatar answered Oct 17 '22 07:10

James


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.

like image 1
John Avatar answered Oct 17 '22 06:10

John