Mark file to delete with Python?

In one of my scripts, I need to delete a file that could be used at that time. I know that I cannot delete a file that is used until it is no longer there, but I also know that I can mark the file for deletion by the operating system (Windows XP). How can I do this in Python?

+3
source share
3 answers
import win32file
import win32api
win32file.MoveFileEx("/path/to/lockedfile.ext", None ,
                 win32file.MOVEFILE_DELAY_UNTIL_REBOOT)
+4
source

... and another version that is independent of the pywin32 binaries.

import ctypes
MOVEFILE_DELAY_UNTIL_REBOOT = 4

ctypes.windll.kernel32.MoveFileExA("/path/to/lockedfile.ext", None,
                                       MOVEFILE_DELAY_UNTIL_REBOOT)
+5
source

MoveFileEx:

dwFlags MOVEFILE_DELAY_UNTIL_REBOOT, lpNewFileName - NULL, MoveFileEx lpExistingFileName, . lpExistingFileName , , .

+1

Source: https://habr.com/ru/post/1770214/


All Articles