Python监控Windows下的文件变化
2023-09-11 14:14:44 时间
windows下监控文件系统的变化。用python非常方便。实例代码例如以下,非常easy。也不多说了。
import os import win32file import win32con ACTIONS = { 1 : "Created", 2 : "Deleted", 3 : "Updated", 4 : "Renamed from something", 5 : "Renamed to something" } FILE_LIST_DIRECTORY = win32con.GENERIC_READ | win32con.GENERIC_WRITE 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 ) if __name__ == '__main__': print("123") while 1: print("321") results = win32file.ReadDirectoryChangesW ( hDir, #handle: Handle to the directory to be monitored. This directory must be opened with the FILE_LIST_DIRECTORY access right. 1024, #size: Size of the buffer to allocate for the results. True, #bWatchSubtree: Specifies whether the ReadDirectoryChangesW function will monitor the directory or the directory tree. 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, file in results: full_filename = os.path.join (path_to_watch, file) print (full_filename, ACTIONS.get (action, "Unknown"))
相关參考链接:
http://docs.activestate.com/activepython/2.4/pywin32/win32file__ReadDirectoryChangesW_meth.html
http://docs.activestate.com/activepython/2.5/pywin32/win32file__CreateFile_meth.html
相关文章
- Python 操作 Kafka --- kafka-python
- Python脚本扫描给定网段的MAC地址表(scapy或 python-nmap)
- How do I run a Python script from C#?
- 【Python】ValueError: unsupported pickle protocol: 5解决方案/同一个项目 python环境尽量保持一致
- Python 文件学习笔记之 File(文件) 对象常用函数
- Word处理控件Aspose.Words功能演示:在 Python 中取消保护 Word 文档
- 【华为OD机试真题 python】投篮大赛【2022 Q4 | 100分】
- Python setup.py和MANIFEST.in文件
- [windows菜鸟]Windows API函数大全(完整)
- 学python好找工作吗?会python可以找什么工作
- 使用python对ES进行批量操作
- python中lambda 表达式(无参数、一个参数、默认参数、可变参数(*args、**kwargs)、带判断的lambda、列表使用lambda)
- 零基础学Python,有些话不知当不当讲?
- windows python web flask获取请求参数数据
- 机器人系统设计与制作:Python语言实现2.4 用LibreCAD生成机器人的二维CAD图
- Python 异常
- 《NLTK基础教程——用NLTK和Python库构建机器学习应用》——第1章 自然语言处理简介
- windows下Python的安装,以及IDLE的使用
- 《Python硬件编程实战》——2.6 如何在Windows系统中安装Python
- Python 使用PyMySql 库 连接MySql数据库时 查询中文遇到的乱码问题(实测可行) python 连接 MySql 中文乱码 pymysql库
- python操作windows窗口获取窗口句柄
- python This application failed to stat could not find or load the Qt platform plugin "windows"
- 【系统运维】开发环境多版本python共存环境部署-Windows
- 华为OD机试 - 热点网络统计(Python) | 机试题+算法思路+考点+代码解析 【2023】
- Windows解决多版本python执行pip3时出错AttributeError: module 'enum' has no attribute 'IntFlag'?
- Python程序设计题库——第六章