Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark File For Removal from Python?

Tags:

python

file

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 can mark the file for removal by the Operating System (Windows XP). How would I do this in Python?

like image 267
Zac Brown Avatar asked Dec 12 '22 18:12

Zac Brown


2 Answers

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

import ctypes
MOVEFILE_DELAY_UNTIL_REBOOT = 4

ctypes.windll.kernel32.MoveFileExA("/path/to/lockedfile.ext", None,
                                       MOVEFILE_DELAY_UNTIL_REBOOT)
like image 55
lunixbochs Avatar answered Jan 03 '23 12:01

lunixbochs


import win32file
import win32api
win32file.MoveFileEx("/path/to/lockedfile.ext", None ,
                 win32file.MOVEFILE_DELAY_UNTIL_REBOOT)
like image 33
Paulo Scardine Avatar answered Jan 03 '23 12:01

Paulo Scardine