Skip to content Skip to sidebar Skip to footer

Mark File For Removal From Python?

In one of my scripts, I need to delete a file that could be in use at the time. I know that I can't remove the file that is in use until it isn't anymore, but I also know that I ca

Solution 1:

...and another version which doesn't depend on pywin32 binaries.

importctypesMOVEFILE_DELAY_UNTIL_REBOOT=4

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

Solution 2:

import win32file
import win32api
win32file.MoveFileEx("/path/to/lockedfile.ext", None ,
                 win32file.MOVEFILE_DELAY_UNTIL_REBOOT)

Solution 3:

Use the MoveFileEx function:

If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and lpNewFileName is NULL, MoveFileEx registers the lpExistingFileName file to be deleted when the system restarts. If lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty.

Post a Comment for "Mark File For Removal From Python?"