your_data = {'rose': 'red',
'sun': 'orange',
'sky': 'blue'}
with open('save.txt','w') as f:
while True:
print(your_data, file=f)
This is an sample program. here the dictionaries are copied to the file named save.txt. Actually I am running this program in raspberry pi and if the power suddenly gets off, the datas stored completely gets erased. I want my program to start again from the previous end of the program or I should have an backup from where I can restart my program.
这是一个示例程序。这里将字典复制到名为save.txt的文件中。实际上我在覆盆子pi中运行这个程序,如果电源突然下降,存储的数据将被完全删除。我希望我的程序从程序的上一端重新开始,或者我应该从我可以重新启动程序的位置进行备份。
1 个解决方案
#1
0
If your program terminates abruptly, buffered output might not make it from your program to the operating system. If power is cut, buffered output might not make it from the operating system to disk.
如果程序突然终止,缓冲输出可能无法从程序到操作系统。如果断电,缓冲输出可能无法从操作系统到磁盘。
The first is handled with flush on the file object, and the second by fsync in the io module.
第一个是在文件对象上使用flush处理,第二个是在io模块中使用fsync处理。
f.flush()
os.fsync(f.fileno())
This is somewhat slow, of course. You can also use open('file.txt', 'w', buffering=0)
to eliminate buffered IO and skip the need to flush (but you will still need to fsync).
当然,这有点慢。您还可以使用open('file.txt','w',buffering = 0)来消除缓冲的IO并跳过刷新的需要(但您仍然需要fsync)。
(Even so, there are still no guarantees: Hard drives are known to report that a write has completed when it is actually on volatile storage inside the hard drive.)
(即使如此,仍然无法保证:当硬盘驱动器实际位于硬盘驱动器内的易失性存储器上时,已知硬盘驱动器报告写入已完成。)
How your program recovers after being interrupted is an entirely different matter. Perhaps it can read the output file to determine where it left off. (When resuming your writes, remember to use append 'a'
mode so as not to truncate your output file.)
你的程序在中断后如何恢复是完全不同的事情。也许它可以读取输出文件以确定它停止的位置。 (恢复写入时,请记住使用追加'a'模式,以免截断输出文件。)
Also try the sqlite3 module: The underlying SQLite database is robust against power failures.
还可以尝试使用sqlite3模块:底层的SQLite数据库可以抵御电源故障。
#1
0
If your program terminates abruptly, buffered output might not make it from your program to the operating system. If power is cut, buffered output might not make it from the operating system to disk.
如果程序突然终止,缓冲输出可能无法从程序到操作系统。如果断电,缓冲输出可能无法从操作系统到磁盘。
The first is handled with flush on the file object, and the second by fsync in the io module.
第一个是在文件对象上使用flush处理,第二个是在io模块中使用fsync处理。
f.flush()
os.fsync(f.fileno())
This is somewhat slow, of course. You can also use open('file.txt', 'w', buffering=0)
to eliminate buffered IO and skip the need to flush (but you will still need to fsync).
当然,这有点慢。您还可以使用open('file.txt','w',buffering = 0)来消除缓冲的IO并跳过刷新的需要(但您仍然需要fsync)。
(Even so, there are still no guarantees: Hard drives are known to report that a write has completed when it is actually on volatile storage inside the hard drive.)
(即使如此,仍然无法保证:当硬盘驱动器实际位于硬盘驱动器内的易失性存储器上时,已知硬盘驱动器报告写入已完成。)
How your program recovers after being interrupted is an entirely different matter. Perhaps it can read the output file to determine where it left off. (When resuming your writes, remember to use append 'a'
mode so as not to truncate your output file.)
你的程序在中断后如何恢复是完全不同的事情。也许它可以读取输出文件以确定它停止的位置。 (恢复写入时,请记住使用追加'a'模式,以免截断输出文件。)
Also try the sqlite3 module: The underlying SQLite database is robust against power failures.
还可以尝试使用sqlite3模块:底层的SQLite数据库可以抵御电源故障。