Python inotify examples

If you are involved in the world of technology even slightly, you will definitely have heard of the programming language known as ‘Python’. In today’s technology-driven world, Python has a vital role to play in making sure that our systems are working as they should. It is one of the most extensively used programming tools out there and for good reason.

As the title suggests, we are going to be talking about a module of Python called ‘inotify’. By the end of the next paragraph, you will know exactly what inotify is and how it is used in practical examples.

Python inotify explained

Python has a large number of in-built libraries that are usable once your Python environment is set up. Since the community is always expanding, not everything can be ingrained in the standard install and it would also take up a lot of unnecessary space. This is where external modules come in and inotify is one of them.

Inotify is a toolkit that specializes in being a watcher for directory and file change. You can give the module a number of paths to observe (starting from one) and whenever there is any change in the files or the directory (like an update, move, delete, new file creation), an alert will be raised which can be used to perform further actions. Inotify is essentially a security guard that raises an alert every time something in the environment changes and reports the activity to the higher command (which is us).

Getting your environment set up

In order to make use of inotify, we will have to install it first. Before we move on to adding inotify, we should confirm if our Python environment is in a stable and working form. The following commands should do the trick: sudo apt-get update && sudo apt-get install python3.6

On a successful run of the commands, your Python environment should have no problem coping with additional installs now. If you still need to verify, you can open the terminal and type python3.6 -v

This command will output the version of Python you have installed and if everything went right, you should be prompted with an output showing the numbers 3.6.

The process of installing additional libraries in Python is very simple and all you have to do is type the following command in the Linux terminal: sudo pip install <module_name> as shown in the picture below.

Once that is done, you should be able to start using inotfiy without any problems. To make sure that we have properly set up the new installations, we will make use of the documentation code that the official inotify page has provided for us. There is a lot of code you can test there to get a better understanding of how the module works. Another way to properly understand its inner workings is to read the documentation in detail. That is possibly one of the best ways in which you can learn about any new module you come across.

Typical applications of inotify

We shall now look at a few real-time examples of how inotify is used by looking at some code snippets and their respective outputs.

The following code segment does 2 things: it tells the program on what actions to give an alert and which directory should it watch out changes for. In this case, the commands are create, delete, modify and move. The directory is /etc

The command after the ampersand symbol lets us create a file in the directory which we have told inotfiy to keep a lookout for and this is the result:

As you can see, an alert was raised on the creation of the file. Now, we will try moving a file into the directory using the following code:

The alert of a file being moved is shown in a very understandable manner and nothing is ambiguous. The result of the file being moved should look like this:

The next command will be of a file being removed and what the inotfiy program will tell us. From the previous examples, we can see that all the outputs have the performed command in upper case letters which lets the user immediately know what happened. The delete command is no different from the others in terms of clarity and you can see it here:

It is a very effective feature that certainly adds value to using inotfiy.

This little tutorial should get you familiar with the ins and outs of how inotify works and you should not have any problem while performing bigger and more complicated tasks. The documentation is very clear and concise about what you have to do and how. This module is not very actively used by other programmers so it can get a little hard to find solutions to specific issues.

About the author

Zeeman Memon

Hi there! I'm a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.