2

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.

user3827728
  • 109
  • 1
  • 1
  • 9

1 Answers1

0

Have you tried using inotify API [1]? There are python interfaces to it surely...

[1] http://man7.org/linux/man-pages/man7/inotify.7.html

f13o
  • 436
  • 2
  • 11
  • Thanks for the reply, I would like to implement this in Windows and inotify is for linux, can I use inotify to do this? – user3827728 Jun 23 '15 at 09:40
  • Try answers from this question: http://stackoverflow.com/questions/3517460/is-there-anything-like-inotify-on-windows – f13o Jun 23 '15 at 22:39