Python API and shell utilities to monitor file system events.
A simple program that uses watchdog to monitor directories specified as command-line arguments and logs events generated:
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Installing from PyPI using pip
:
$ pip install watchdog
Installing from PyPI using easy_install
:
$ easy_install watchdog
Installing from source:
$ python setup.py install
You can browse the latest release documentation online.
Fork the repository on GitHub and send a pull request, or file an issue ticket at the issue tracker. For general help and questions use the official mailing list or ask on stackoverflow with tag python-watchdog.
Create and activate your virtual environment, then:
pip install pytest pip install -e . py.test tests
- Linux 2.6 (inotify)
- Mac OS X (FSEvents, kqueue)
- FreeBSD/BSD (kqueue)
- Windows (ReadDirectoryChangesW with I/O completion ports; ReadDirectoryChangesW worker threads)
- OS-independent (polling the disk for directory snapshots and comparing them periodically; slow and not recommended)
Note that when using watchdog with kqueue, you need the
number of file descriptors allowed to be opened by programs
running on your system to be increased to more than the
number of files that you will be monitoring. The easiest way
to do that is to edit your ~/.profile
file and add
a line similar to:
ulimit -n 1024
This is an inherent problem with kqueue because it uses file descriptors to monitor files. That plus the enormous amount of bookkeeping that watchdog needs to do in order to monitor file descriptors just makes this a painful way to monitor files and directories. In essence, kqueue is not a very scalable way to monitor a deeply nested directory of files and directories with a large number of files.
Vim does not modify files unless directed to do so. It creates backup files and then swaps them in to replace the files you are editing on the disk. This means that if you use Vim to edit your files, the on-modified events for those files will not be triggered by watchdog. You may need to configure Vim to appropriately to disable this feature.
- Python 2.6 or above.
- pathtools
- select_backport (select.kqueue replacement for 2.6 on BSD/Mac OS X)
- XCode (only on Mac OS X)
Watchdog is licensed under the terms of the Apache License, version 2.0.
Copyright 2011 Yesudeep Mangalapilly.
Copyright 2012 Google, Inc.
Project source code is available at Github. Please report bugs and file enhancement requests at the issue tracker.
Too many people tried to do the same thing and none did what I needed Python to do: