I want to monitor certain files inside the folder, when ever there is file change, I would like to capture that change and based on this change I want to trigger certain program.
Initially I referred stack overflow and used some code from that. When I try to implement, this is not able to trigger a certain program but it is capturing the change alone.
Please find my code below:
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
I kindly request anyone to help me in this. Thanks in advance.