Many times a file must be processed during its creation or modification. This can be done by following the changes in a specific directory. There are many ways in python to keep track of changes made to a directory. One such way is to use the watchdog
module. As the name suggests, this module monitors the given directory and can notify if a file is created or modified.
Required Modules
- Guard Dog
To install the watchdog, run this command in a terminal.pip install watchdog
- logging
It comes with Python, so no need install it externally.
Now let’s dive into the code that will log all the logged changes. This code will only log changes checked in to the current directory.
| tr>
Output:
The above code will log all changes registered to the directory. If you want to make changes while creating or modifying a file, you can do so using the following code.
|
Output: