Python API and shell utilities to monitor file system events.
This is a fork of http://github.com/gorakhargosh/watchdog for Python 3 and intended for Nuxeo Drive. This is not an official Watchdog nextgen repository but it will be kept alive by Nuxeo and PR are welcome.
This is a drop-in replacement of the original watchdog module. Only thee module name changes at installation time.
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 watchdog3
Installing from PyPI using easy_install
:
$ easy_install watchdog3
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:
python -m pip install --user pytest python -m pip install -e . python -m pytest 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.
Watchdog is licensed under the terms of the Apache License, version 2.0.
Copyright 2011 Yesudeep Mangalapilly.
Copyright 2012 Google, Inc.
Copyright 2018 Nuxeo.
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: