删除 Python 程序留下的 shelve .dat 文件的简单方法?
所以我有一个 python 程序,在执行后最终会留下一个来自 shelve
函数的 .dat 文件。我希望我的程序在完成后删除或清除该文件。我的教科书只提到如何创建 .dat 文件,但没有提到如何清除它。有什么好的命令可以解决这个问题吗?程序运行完成后,我不再需要 .dat 文件。
So I have a python program that ends up leaving a .dat file from the shelve
function behind after execution. I would like my program to delete or clear that file once it is done. My textbook only mentions how to create a .dat file but not how to clear it. Any good commands out there to take care of this? I don't need the .dat file again after my program runs to completion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很简单:
当 Python 解释器以正常(未终止/崩溃)方式存在时运行 os.remove( path_to_file ) 。但您需要确保该文件届时已关闭。
This is easy:
runs
os.remove( path_to_file )
when the Python interpreter exists in a normal (not killed/crashed) way. But you need to make sure the file is closed by then.注册一个
atexit
处理程序来为您进行清理(如文档在这里)。Register an
atexit
handler to do the cleanup for you (as described in the documentation here).