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. 03-组合逻辑电路设计之译码器——小梅哥FPGA设计思想与验证方法视频教程配套文档

    芯航线——普利斯队长精心奉献 课程目标:    1. 再次熟悉Quartus II工程的建立以及完整的FPGA开发流程 2. 以译码器为例学会简单组合逻辑电路设计 实验平台:无 实验原理: 组合逻辑, ...

  2. Unity3D中Console控制台的扩展

    Assert Store上有一个Editor Console Pro,功能非常全面,百度也能搜到破解.如果有需要建议使用,不要再造车轮 起初因为自带Console功能太弱,有不少可以提升空间.于是尝试 ...

  3. A feature in Netsuite Reports > Financial > Balance Sheet

    最新版本的Customize balance sheet page Left side > Layout > Add Reference Row Then in right side, y ...

  4. C++去掉字符串首尾的 空格 换行 回车

    /* *去掉字符串首尾的 \x20 \r \n 字符 */ void TrimSpace(char* str) { ; char *end = str; char *p = str; while(*p ...

  5. PHP 用户登录与退出

    PHP 用户登录与退出 登录页面 login.html 负责收集用户填写的登录信息. <fieldset> <legend>用户登录</legend> <fo ...

  6. memcache内存估算整理

    参考文章: http://blog.csdn.net/tonyxf121/article/details/7906428 http://zhihuzeye.com/archives/2361 memc ...

  7. ThinkPHP 3.2 模板中的Angularjs 的变量{{$first}} 无法被解析

    ThinkPHP 3.2 模板中的Angularjs 的变量"{{$first}}" 无法被解析, 模板解析冲突,例如在angularjs 的变量"{{$first}}& ...

  8. linux笔记:shell基础-环境变量配置文件

    source命令(重新读入配置文件,不用重启就直接生效): 环境变量配置文件: linux中的环境变量配置文件(~代表当前用户的家目录): 配置文件读取顺序: /etc/profile 文件的作用:

  9. AxureRP8实战手册(基础11-20)

    本文目录 基础11. 设置文本框输入为密码 基础12. 设置打开选择文件窗口 基础13. 限制文本框输入字符位数 基础14. 设置文本框提示文字 基础15. 设置文本框回车触发事件 基础16. 设置元 ...

  10. 自定义泛型N维空间数组

    class Space<T> : IEnumerable<Space<T>> { public T Filler { get { if (!ed) { ed = t ...