- Notifications
You must be signed in to change notification settings - Fork709
Python library and shell utilities to monitor filesystem events.
License
Apache-2.0, Unknown licenses found
Licenses found
gorakhargosh/watchdog
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Python API and shell utilities to monitor file system events.
Works on 3.9+.
A simple program that uses watchdog to monitor directories specifiedas command-line arguments and logs events generated:
importtimefromwatchdog.eventsimportFileSystemEvent,FileSystemEventHandlerfromwatchdog.observersimportObserverclassMyEventHandler(FileSystemEventHandler):defon_any_event(self,event:FileSystemEvent)->None:print(event)event_handler=MyEventHandler()observer=Observer()observer.schedule(event_handler,".",recursive=True)observer.start()try:whileTrue:time.sleep(1)finally:observer.stop()observer.join()
Watchdog comes with anoptional utility script calledwatchmedo
.Please typewatchmedo --help
at the shell prompt toknow more about this tool.
Here is how you can log the current directory recursivelyfor events related only to*.py
and*.txt
files whileignoring all directory events:
watchmedo log \ --patterns='*.py;*.txt' \ --ignore-directories \ --recursive \ --verbose \.
You can use theshell-command
subcommand to execute shell commands inresponse to events:
watchmedo shell-command \ --patterns='*.py;*.txt' \ --recursive \ --command='echo "${watch_src_path}"' \.
Please see the help information for these commands by typing:
watchmedo [command] --help
watchmedo
can readtricks.yaml
files and execute tricks within them inresponse to file system events. Tricks are actually event handlers thatsubclasswatchdog.tricks.Trick
and are written by plugin authors. Trickclasses are augmented with a few additional features that regular event handlersdon't need.
An exampletricks.yaml
file:
tricks:-watchdog.tricks.LoggerTrick:patterns:["*.py", "*.js"]-watchmedo_webtricks.GoogleClosureTrick:patterns:['*.js']hash_names:truemappings_format:json# json|yaml|pythonmappings_module:app/javascript_mappingssuffix:.min.jscompilation_level:advanced# simple|advancedsource_directory:app/static/js/destination_directory:app/public/js/files:index-page: -app/static/js/vendor/jquery*.js -app/static/js/base.js -app/static/js/index-page.jsabout-page: -app/static/js/vendor/jquery*.js -app/static/js/base.js -app/static/js/about-page/**/*.js
The directory containing thetricks.yaml
file will be monitored. Each trickclass is initialized with its corresponding keys in thetricks.yaml
file asarguments and events are fed to an instance of this class as they arrive.
Install from PyPI usingpip
:
$ python -m pip install -U watchdog# or to install the watchmedo utility:$ python -m pip install -U'watchdog[watchmedo]'
Install from source:
$ python -m pip install -e.# or to install the watchmedo utility:$ python -m pip install -e'.[watchmedo]'
You can browse the latest releasedocumentation online.
Fork therepository on GitHub and send a pull request, or file an issueticket at theissue tracker. For general help and questions usestackoverflow with tag python-watchdog.
Create and activate your virtual environment, then:
python -m pip install toxpython -m tox [-q] [-e ENV]
If you are making a substantial change, add an entry to the "Unreleased" sectionof thechangelog.
- Linux 2.6 (inotify)
- macOS (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 themperiodically; slow and not recommended)
Note that when using watchdog with kqueue, you need thenumber of file descriptors allowed to be opened by programsrunning on your system to be increased to more than thenumber of files that you will be monitoring. The easiest wayto do that is to edit your~/.profile
file and adda line similar to:
ulimit -n 1024
This is an inherent problem with kqueue because it usesfile descriptors to monitor files. That plus the enormousamount of bookkeeping that watchdog needs to do in orderto monitor file descriptors just makes this a painful wayto monitor files and directories. In essence, kqueue isnot a very scalable way to monitor a deeply nesteddirectory of files and directories with a large number offiles.
Vim does not modify files unless directed to do so.It creates backup files and then swaps them in to replacethe files you are editing on the disk. This means thatif you use Vim to edit your files, the on-modified eventsfor those files will not be triggered by watchdog.You may need to configure Vim appropriately to disablethis feature.
When you want to watch changes in CIFS, you need to explicitly tell watchdog tousePollingObserver
, that is, instead of letting watchdog decide anappropriate observer like in the example above, do:
from watchdog.observers.polling import PollingObserver as Observer
Watchdog is licensed under the terms of theApache License, version 2.0.
- Copyright 2018-2025 Mickaël Schoentgen & contributors
- Copyright 2014-2018 Thomas Amland & contributors
- Copyright 2012-2014 Google, Inc.
- Copyright 2011-2012 Yesudeep Mangalapilly
Projectsource code is available at Github. Please report bugs and fileenhancement requests at theissue tracker.
Too many people tried to do the same thing and none did what I needed Pythonto do:
About
Python library and shell utilities to monitor filesystem events.