Save to Text File from Infinite While Loop

China☆狼群 提交于 2019-12-22 08:28:03

问题


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?


回答1:


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 )
    flle.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.




回答2:


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.




回答3:


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




回答4:


close the file and write the code again to the file. and try choosing a+ mode



来源:https://stackoverflow.com/questions/16863857/save-to-text-file-from-infinite-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!