Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Python monitors Music folder events

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how Python monitors music folder events. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can gain something through the detailed introduction of this article.

Now there is an application scenario that needs to monitor the file system, generate logs when changes occur, and do some corresponding operations on the new files. For example, apply to our previous best part extractor: if a music file is added to the current folder, the monitor will call the best part extractor to extract the best part of the music file.

Such a monitor is not difficult to write, but it takes a lot of time and there are many situations to consider. But fortunately we write Python, there are a lot of wheels to use.

1. Install "watchdog"

The watchdog module is a Python "wheel" for monitoring changes in file events. It has an excellent code architecture and can register many event handlers to facilitate users to do custom operations. If you have not installed Python, please read this article, "detailed installation Guide for Python". Enter the following command at the terminal to install the watchdog:

Pip install watchdog

two。 Basic use

The use of watchdogs is not complicated, please take a closer look at the following code and comments:

You can see that there are several key steps in the code

1. Configure various information

two。 Generate event handlers, monitors

3. Register event handlers, configure directories, recursive execution (that is, monitor subfolders at the same time)

4: start.

In fact, the watchdog's observer is based on the threading.Thread object, so many properties of observer inherit the properties of threading.Thread.

If you run the script without parameters, you want to monitor the folder where the script files are located. If you want to monitor other folders, remember to run with the path parameters of the folder, such as:

Python obserber.py / data/home/ckend/

Let's try to run it:

As you can see, everything I do under the current folder is recorded. Next, let's try how to customize some actions.

3. Monitor to extract the best part of the music

If you don't know how to extract the best part of a music file, please read this article: "Python automatically extracts the best part of a music file."

There are several ways to do this. One is to make some new changes to the original log processor, such as adding a function to call the best part of the music extractor. The second is to re-inherit the FileSystemEventHandler class and modify it accordingly. Here we still want to keep the style of log, just to extract the best part of the music during log, so we use the first method.

Look at the on_created in the LoggingEventHandler source code, which is what the monitor does when the file is created:

We just need to inherit this class and modify on_created to do what we want:

First declare a class, inherit LoggingEventHandler, and then overload the on_created function, which not only records the file event changes, but also extracts the best part of the music from the mp3 file. Finally, don't forget to generate the event handler with our new class name.

To see the effect, copy the small forever .mp3:

Successfully monitor the changes of the file and extract the best part of the music to generate the best part of the file. In this way, as long as you keep the Python process open, it will always monitor this folder. Once music files enter, it will automatically extract the best part of its music. In linux system, it can be used with supervisor, which is very easy to use.

The above is how Python monitors music folder events. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report