搬运:Python for Windows——监控Windows某个目录下文件的变化
https://win32com.goermezer.de/content/view/286/285/
这个网站真是给力,不多说,代码直接搬运过来,还有我的测试结果,拿走不谢!
import os import sys 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 = 'E:/test' print 'Watching changes in', 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, filename in results: full_filename = os.path.join(path_to_watch, filename) print full_filename, ACTIONS.get(action, "Unknown")
运行结果:
到这一步,后面可以做得就多了,可以写LOG,做统计,做预警,太爽啦!