How to monitor changes in a folder?

3

How can I monitor and capture changes to folders and files? I intend to use this as part of a service that starts with the system the script would be in .pyw . example:

if(mudou != padrao):
    pass

But I do not know how I would do this script to monitor the changes of this folder, I thought to do with sleep() but I do not know if there would be a proper module for this.

    
asked by anonymous 08.09.2016 / 17:14

1 answer

2

You can use the Watchdog

Install:

> pip install watchdog 

Example:

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):
    def on_any_event(self, event):
        print 'Evento', event.event_type,' caminho:', event.src_path, 'diretorio?', event.is_directory

path = 'C:\caminho\desejado\aqui'
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')
observer = Observer()
observer.schedule(MyHandler(), path, recursive=True)
observer.start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()
observer.join()
    
08.09.2016 / 18:47