Created
September 20, 2017 14:28
-
-
Save costastf/c9065c86261b67c4ae435f92d47997bc to your computer and use it in GitHub Desktop.
Proof of concept to watch a directory for changes with python on windows XP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import win32file | |
import win32con | |
ACTIONS = { | |
1: "Created", | |
2: "Deleted", | |
3: "Updated", | |
4: "Renamed from something", | |
5: "Renamed to something" | |
} | |
FILE_LIST_DIRECTORY = 0x0001 | |
path_to_watch = "." | |
hDir = win32file.CreateFile( | |
path_to_watch, | |
FILE_LIST_DIRECTORY, | |
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, | |
None, | |
win32con.OPEN_EXISTING, | |
win32con.FILE_FLAG_BACKUP_SEMANTICS, | |
None | |
) | |
while 1: | |
results = win32file.ReadDirectoryChangesW( | |
hDir, | |
1024, | |
True, | |
win32con.FILE_NOTIFY_CHANGE_FILE_NAME | | |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME | | |
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES | | |
win32con.FILE_NOTIFY_CHANGE_SIZE | | |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE | | |
win32con.FILE_NOTIFY_CHANGE_SECURITY, | |
None, | |
None | |
) | |
for action, file in results: | |
full_filename = os.path.join(path_to_watch, file) | |
print full_filename, ACTIONS.get(action, "Unknown") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment