开发者

Easy way to delete a shelve .dat file left behind by my Python program?

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.


This is easy:

import sys, os
sys.atexit.register( os.remove, path_to_file )

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.


Register an atexit handler to do the cleanup for you (as described in the documentation here).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜