我想监视文件夹中的某些文件,当有文件更改时,我想捕获该更改并根据此更改我想触发某些程序。
最初我引用了堆栈溢出并使用了一些代码。当我尝试实现时,这不能触发某个程序,但它正在单独捕获更改。
请在下面找到我的代码:
import os, sys, time
from time import gmtime, strftime
def files_to_timestamp(path):
files = [os.path.join(path, f) for f in os.listdir(path)]
return dict ([(f, os.path.getmtime(f)) for f in files])
if __name__ == "__main__":
path_to_watch = r"Desktop\Test\"
print "Watching ", path_to_watch
before = files_to_timestamp(path_to_watch)
while 1:
time.sleep (2)
after = files_to_timestamp(path_to_watch)
added = [f for f in after.keys() if not f in before.keys()]
removed = [f for f in before.keys() if not f in after.keys()]
modified = []
for f in before.keys():
if not f in removed:
if os.path.getmtime(f) != before.get(f):
modified.append(f)
if added: print "Added: ", ", ".join(added)
if removed: print "Removed: ", ", ".join(removed)
if modified:
file1 = ", ".join(modified)
print "Updated:",strftime("%Y-%m-%d %H:%M:%S", gmtime()),file1
if file1 ==
'Desktop\Test\L90FLA_TSMC_BE_SBE_THEBE_LOT_MONTHLY_Test.eff':
execfile(r'H:\Python programs\PSBE_THEBE_New requirments.py')
elif file1 ==
'Desktop\Test\L90FLA_TSMC_BE_SBE_CARME_MONTHLY_Test.eff':
execfile(r'H:\Python programs\PSBE_CARME_New requirments.py')
elif file1 ==
'Desktop\Test\L90FLA_TSMC_BE_SBE_ELARA_LOT_MONTHLY_Test.eff':
execfile(r'H:\Python programs\PSBE_ELARA_New requirments.py')
before = after
我请求任何人帮助我。提前谢谢。
答案 0 :(得分:0)
您是否尝试过使用inotify API [1]?肯定有它的python接口......