문제

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top