import win32file
import tempfile
import threading
import win32con
import os dirs=["C:\\WINDOWS\\TEMP",tempfile.gettempdir()]
def start_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 )
while True:
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)
for action, filename in results:
print action
print filename
except:
pass for path in dirs:
monitor_thread = threading.Thread(target=start_monitor,args=(path,))
monitor_thread.start()

python多线程监控指定目录,主要函数是

win32file.ReadDirectoryChangesW监控目录变化

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 be 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 will 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 has completed. This parameter can be one or more of the FILE_NOTIFY_CHANGE_* values.

overlapped=None : PyOVERLAPPED

An overlapped object. The directory must also be opened with FILE_FLAG_OVERLAPPED.

python多线程监控指定目录的更多相关文章

  1. python之对指定目录文件夹的批量重命名

    python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" ...

  2. 使用WatchService监控指定目录内的文件的改动

    package coin; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Pat ...

  3. C#监控指定目录的文件变化的代码

    如下的资料是关于C#监控指定目录的文件变化的代码. FileSystemWatcher watcher = new FileSystemWatcher();watcher.Path = @" ...

  4. [Erlang27]如何监控指定目录下的*.beam文件,如果有改动就更新到指定的节点?

    在Erlang In Anger第二章中讲到使用rebar来创建一个Erlang项目(Application或Project) 但美中不足的只是给出了指引,但没有给出详细的步骤. 下面我们就使用reb ...

  5. linux下python安装到指定目录

    由于使用公司服务器时没有root权限,只能把python安装到个人文件夹下,使用源码包方式安装,这里记录一下. 1.python下载 cd到目录/users/w,在此目录下安装python.通过wge ...

  6. VC++ 监控指定目录改变

    转载:http://www.cnblogs.com/doublesnke/archive/2011/08/16/2141374.html VC++实施文件监控:实例和详解 相关帮助: http://h ...

  7. Python批量删除指定目录下的指定类型的文件

    Python作为一种脚本语言.其很适合文件级的各种操作.以下的代码能够批量删除指定目录下的所有特定类型(CSV类型)的文件. import sys, csv , operator import os ...

  8. Python —— 批量替换指定目录下的所有文件中指定字符串

    参考:http://blog.csdn.net/zcwfengbingdongguke/article/details/13951527 代码: #!/usr/bin/python import os ...

  9. python之删除指定目录指定日期下的日志文件

    #=======================================================================================20190521以下脚本 ...

随机推荐

  1. win7 64+python2.7.12安装numpy+scipy+matplotlib+scikit-learn

    python包下载网址 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 1.已经安装python2.7.12,查看scripts里是否有pip.2.7.9版本以上 ...

  2. KVM虚拟机virsh常用参数

    线上虚拟机挂了,常用的命令忘了,记录下 链接:http://www.ibm.com/support/knowledgecenter/zh/linuxonibm/liabp/liabpcommonvir ...

  3. Sublime Text怎么在切分两行视口内显示同一个文件

    原文链接:http://devlog.en.alt-area.org/?p=1098 How to split one file into two views in Sublime Text2 You ...

  4. [四校联考P3] 区间颜色众数 (主席树)

    主席树 Description 给定一个长度为 N 颜色序列A,有M个询问:每次询问一个区间里是否有一种颜色的数量超过了区间的一半,并指出是哪种颜色. Input 输入文件第一行有两个整数:N和C 输 ...

  5. [问题2015S12] 复旦高等代数 II(14级)每周一题(第十三教学周)

    [问题2015S12]  设 \(A\) 为 \(n\) 阶实矩阵, 若对任意的非零 \(n\) 维实列向量 \(\alpha\), 总有 \(\alpha'A\alpha>0\), 则称 \( ...

  6. CentOS7 续续

    1.配置网络,虚拟机为桥接模式,IP地址为 192.168.100+学号/24,配置完成后可以通过ping物理机192.168.100段,或者ping 192.168.100.140验证2.通过临时与 ...

  7. 提取data.frame中的部分数据(不含列标题和行标题)

    ?unlist     Given a list structure x, unlist simplifies it to produce a vector which contains all th ...

  8. Sprint(第二天11.15)

    Sprint1第一阶段 1.类名:软件工程-第一阶段 2.时间:11.14-11.23 3.选题内容:点餐系统 4.团队博客地址:http://www.cnblogs.com/iamCarson/ 团 ...

  9. IE的条件注释

    <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> <!--[if IE]> 所有的IE可识别 <![ ...

  10. FreeMarker常用语法

    转自:http://www.cnblogs.com/linjiqin/p/3388298.html FreeMarker的插值有如下两种类型:1,通用插值${expr};2,数字格式化插值:#{exp ...