我有一个基于计时器的应用程序,它可能每 8 小时向文件写入一小行。该文件不会在 8 小时内保持打开状态,我只是打开它,写入并关闭它。如果有帮助,我打开文件仅用于写入,而不是追加,因此不需要保存其中以前的数据。
如果用户在文件打开用于写入时通过任务管理器关闭应用程序,会发生什么情况?我可以使文件写入操作原子化吗?或者,如果文件打开,我至少可以阻止应用程序关闭吗?
最佳答案
What would happen if the user closes the application through task manager while the file is open for writing?
除非您有程序的退出处理程序,否则程序很可能会立即关闭。如果用户终止该程序,它将立即关闭。
Can I make the file writing operation atomic?
我不确定你所说的“原子”是什么意思,但这里有一个可以帮助你的链接:atomic writing to file with Python
Or can I at least prevent the application from closing if the file is open?
如果用户终止进程,您无法阻止程序关闭。
关于python - 在Python中打开文件时防止应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38585259/