1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
.. include:: global.rst.inc
.. _quickstart:
Quickstart
==========
Below we present a simple example that monitors the current directory
recursively (which means, it will traverse any sub-directories)
to detect changes. Here is what we will do with the API:
1. Create an instance of the :class:`watchdog.observers.Observer` thread class.
2. Implement a subclass of :class:`watchdog.events.FileSystemEventHandler`.
3. Schedule monitoring a few paths with the observer instance
attaching the event handler.
4. Start the observer thread and wait for it generate events
without blocking our main thread.
By default, an :class:`watchdog.observers.Observer` instance will not monitor
sub-directories. By passing ``recursive=True`` in the call to
:meth:`watchdog.observers.Observer.schedule` monitoring
entire directory trees is ensured.
A Simple Example
----------------
The following example program will monitor the current directory recursively for
file system changes and simply print them to the console::
import time
from watchdog.events import FileSystemEvent, FileSystemEventHandler
from watchdog.observers import Observer
class MyEventHandler(FileSystemEventHandler):
def on_any_event(self, event: FileSystemEvent) -> None:
print(event)
event_handler = MyEventHandler()
observer = Observer()
observer.schedule(event_handler, ".", recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
To stop the program, press Control-C.
Typing
------
If you are using type annotations it is important to note that
:class:`watchdog.observers.Observer` is not actually a class; it is a variable that
hold the "best" observer class available on your platform.
In order to correctly type your own code your should use
:class:`watchdog.observers.api.BaseObserver`. For example::
from watchdog.observers import Observer
from watchdog.observers.api import BaseObserver
def my_func(obs: BaseObserver) -> None:
# Do something with obs
pass
observer: BaseObserver = Observer()
my_func(observer)
|