ImportWin32fileImportTempfileImportThreadingImportWin32conImportOsdirs=["c:\\windows\\temp", Tempfile.gettempdir ()]defStart_monitor (path_to_watch): H_directory=Win32file. CreateFile (Path_to_watch, Win32con. Generic_read, Win32con. File_share_delete|win32con. file_share_read|Win32con. File_share_write, None, Win32con. Open_existing, Win32con. File_flag_backup_semantics, None) whileTrue:Try: Results= Win32file. READDIRECTORYCHANGESW (h_directory,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) forAction, filenameinchResults:PrintActionPrintfilenameexcept: Pass forPathinchDirs:monitor_thread= Threading. Thread (target=start_monitor,args=(path,)) Monitor_thread.start ()
Python multithreaded monitoring Specifies the directory, the main function is
Win32file. READDIRECTORYCHANGESW Monitoring Directory Changes
READDIRECTORYCHANGESW (handle, size, Bwatchsubtree, Dwnotifyfilter , overlapped)
Retrieves information describing the changes occurring within a directory.
Parameters
Handle : Pyhandle
Handle to the directory to be monitored. This directory must is opened with the File_list_directory access right.
size : int
Size of the buffer to allocate for the results.
bwatchsubtree : int
Specifies whether the READDIRECTORYCHANGESW function would monitor the directory or the directory tree. If TRUE is specified, the function monitors the directory tree rooted at the specified directory. If FALSE is specified, the function monitors only the directory specified by the hdirectory parameter.
dwnotifyfilter : int
Specifies filter criteria the function checks to determine if the wait operation have completed. This parameter can is one or more of the file_notify_change_* values.
overlapped=none : pyoverlapped
An overlapped object. The directory must also is opened with file_flag_overlapped.
Python multithreaded monitoring specified directory