监控多文件夹目录变化,python,win32

# -*- coding: utf-8 -*-
"""
Created on Tue Mar  3 21:40:33 2020
@author: HomeLocal
"""
import os
import threading
import win32file
import win32con

ACTIONS = {
    1: "Created",
    2: "Deleted",
    3: "Updated",
    4: "Renamed from something",
    5: "Renamed to something"
}

FILE_LIST_DIRECTORY = 0x0001


def simpleFuction(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)
            # full_filename=full_filename.decode('unicode_escape').encode('utf-8')
            print(full_filename, ACTIONS.get(action, "Unknown"))
# path_to_watch = 'D:\\file1'
# path_to_watch2 = 'D:\\file2'
# print('Watching changes in', path_to_watch)
# simpleFuction(path_to_watch)
path=["D:\\file1","D:\\file2"]
threads = []
for i in path:
    t1 = threading.Thread(target=simpleFuction,args=(i,))
    threads.append(t1)
for t in threads:
    t.setDaemon(True)
    t.start()

for t in threads:
    t.join()
# if __name__ == '__main__':

参看虫师:python 多线程就这么简单 - 虫师 - 博客园 (cnblogs.com)

忘了你的地址了,保留了原作者的名字:

author: HomeLocal
posted @ 2021-07-26 14:02  死磕战队  阅读(234)  评论(0编辑  收藏  举报