The problem you are facing is not with Python, but with Windows. This can be done, but you will have to write non-trival C / C ++ code for it.
A file or file closing notification opens in user mode in Windows. Therefore, libraries offered by others do not have a notification about closing the file. The Windows API for detecting changes in userland is ReadDirectoryChangesW . He will warn you of one of the following notifications :
FILE_ACTION_ADDED if the file was added to the directory.FILE_ACTION_REMOVED if the file was deleted from the directory.FILE_ACTION_MODIFIED if the file was modified. This may be a change in timestamp or attributes.FILE_ACTION_RENAMED_OLD_NAME if the file has been renamed, and this is the old name.FILE_ACTION_RENAMED_NEW_NAME if the file has been renamed, and this is a new name.
No amount of Python can change what Windows provides you.
To receive notification of file closure, tools such as Process Monitor , install the Minifilter, which lives in the kernel , next to other filters, such as EFS.
To achieve what you want, you need to:
- Install Minifilter, which has code to send events back to userland. Use the Microsoft Minispy sample , it is stable and fast.
- Convert the code from the
user program to make it a Python extension ( minispy.pyd ), which provides a generator that generates events. This is the difficult part, I will return to this. - You will need to filter events, you will not accept the amount of input / output in an unoccupied Windows window!
- Your Python program can then import the extension and execute it.
Everything looks something like this:

Of course, you can have EFS via NTFS, this is just to show that your minifilter would be above everything.
Hard parts:
- Your minifilter must be digitally signed by an authority trusted by Microsoft. Verification comes to mind, but there are others.
- Debugging requires a separate (virtual) machine, but you can easily customize your interface.
- You will need to install a mini-filter with an account with administrator rights. Any user will be able to read events.
- You will have to deal with multiple users. For many users, there is only one mini-filter.
- You will need to convert the user program from the MiniSpy sample to a DLL that you will wrap with the Python extension.
The last two are the most difficult.
ixe013
source share