我想编写一个 python 脚本(并在后台在 windows 7 上 24/7 运行它)检查是否在 c:\Data\ 或 c:\Data 的子目录中创建了一个新目录(例如 c:\Data \1\2\3\ 或 c:\Data\1\test\2)
跟踪目录创建的最高效方式(在 Windows 7 上)是什么?
这是一个快速扫描工具,每 10 秒扫描一次更改:
>>> import os
>>> from time import sleep
>>> def build_dir_tree(base):
all_dirs = []
for root, dirs, files in os.walk(base):
for dir_ in dirs:
all_dirs.append(os.path.join(root, dir_))
return all_dirs
>>> base = r'E:\CFM\IT\Dev\Python\test'
>>> old_dirs = build_dir_tree(base)
>>> while True:
new_dirs = build_dir_tree(base)
removed = [d for d in old_dirs if d not in new_dirs]
added = [d for d in new_dirs if d not in old_dirs]
print 'Added :', added, '- Removed:', removed
old_dirs = new_dirs
sleep(10)
Added : [] - Removed: []
Added : ['E:\\CFM\\IT\\Dev\\Python\\test\\hep'] - Removed: []
Added : [] - Removed: []
Added : ['E:\\CFM\\IT\\Dev\\Python\\test\\hop\\hap'] - Removed: []
Added : [] - Removed: ['E:\\CFM\\IT\\Dev\\Python\\test\\hep']
你只需要根据你的时间步长、时间显示等来调整它。