0

The goal is to do something every time a file from a list is modified (saved).

I have no idea to proceed. The end goal should be something like this:

files = ['file1.txt', 'file2.txt', 'file3,txt']

if (one of the files in files is modified):
    Print '%s has been modified' % (filename)
tshepang
  • 12,111
  • 21
  • 91
  • 136
VictorVH
  • 327
  • 1
  • 4
  • 14

1 Answers1

1

This will check the times using a dict and print any modified files:

 import os.path, time
files = ['file1.txt', 'file2.txt', 'file3,txt']
changes =  {"file1.txt":os.path.getmtime("file1.txt"),"file2.txt":os.path.getmtime("file2.txt"),"file3.txt":os.path.getmtime("file3.txt")}
while True:
    for f in files:
        if changes.get(f) < os.path.getmtime(f):
            print "File {} has been modified".format(f)
            changes[f] = os.path.getmtime(f)
        else:
            print "No changes, going to sleep."
    time.sleep(10)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321