Here is a general solution that, when called, will wait FOREVER until the directory is changed. This function can be called before any code that will do something in the directory, for example, count the number of files, etc. It can be used to block execution until the dir changes:
def directory_modified(dir_path, poll_timeout=30): import os import time init_mtime = os.stat(dir_path).st_mtime while True: now_mtime = os.stat(dir_path).st_mtime if init_mtime != now_mtime: return True time.sleep(poll_timeout)
Please note that you can overwrite the timeout, the default is 30 seconds. Here is the function used:
>>> att_dir = '/data/webalert/attachments' >>> directory_modified(att_dir, 5)
The function returns true after a maximum runtime of 5 seconds in case of sleep start, as soon as I changed the directory. Hope this helps those who need a common approach.
source share