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. [课程设计]Scrum 多鱼点餐系统(团队交流日)

    [课程设计]Scrum  多鱼点餐系统(团队交流日) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系统WEB ...

  2. Locality Sensitive Hash 局部敏感哈希

    Locality Sensitive Hash是一种常见的用于处理高维向量的索引办法.与其它基于Tree的数据结构,诸如KD-Tree.SR-Tree相比,它较好地克服了Curse of Dimens ...

  3. 有向图的强连通分量——Tarjan

    在同一个DFS树中分离不同的强连通分量SCC; 考虑一个强连通分量C,设第一个被发现的点是 x,希望在 x 访问完时立刻输出 C,这样就可以实现 在同一个DFS树中分离不同的强连通分量了. 问题就转换 ...

  4. js中的什么时候需要用new来实例化?

    有人说js中函数和类就是一个概念,请问:1 为什么我们在大多数情况下没有用new来实例化一个类(函数),如下 JavaScript code   1 2 3 4 5 6 7 <script> ...

  5. C++高精度计算代码运行时间(转载)

    转载:http://blog.csdn.net/rrrfff/article/details/6583410 //在定时前应该先调用QueryPerformanceFrequency()函数获得机器内 ...

  6. [问题2014A02] 解答三(降阶公式法)

    [问题2014A02] 解答三(降阶公式法) 将矩阵 \(A\) 写成如下形式: \[A=\begin{pmatrix} -2a_1 & 0 & \cdots & 0 & ...

  7. for与if、else的使用

    #!/usr/bin/env python def fun3(x) : num =0 for i in x : if i == "" : num += 1 if num == 0 ...

  8. ArrayList代码示例

    package com.shushine.framework.第七章Java标准类库;import java.util.ArrayList;/** * * <p> * 描述该类情况 {@l ...

  9. DSO、CUBE区别(覆盖、合计)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. PHP-----类与对象,成员方法,成员属性,构造方法,析构方法

    php里面的类-----class XX{},通过类的定义,可以使用调用类里面的成员属性和成员方法. 对象---一个类就是一个对象,一个对象可以有多个属性,一个类可以有多个成员方法. 构造方法:一个类 ...