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?
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With