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 to monitor file changes in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about how to monitor file changes in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

1. Prepare for

Before you begin, make sure that Python and pip are successfully installed on your computer. Oh, if not, please visit this article: hyperdetailed Python installation Guide for installation. If you use Python for data analysis, you can directly install Anaconda:Python data analysis and mining helper-Anaconda

Open Cmd (start-run-CMD) in Windows environment, and open Terminal (command+ space input Terminal) in Apple system environment, ready to start entering commands to install dependencies.

Of course, I recommend that you use the VSCode editor, Copy the code of this article, and install the dependency module by running commands in the terminal below the editor. What a comfortable thing: the best partner of Python programming-VSCode detailed guide.

Enter the following command at the terminal to install the dependent module we need:

Pip install watchdog

If you see Successfully installed xxx, the installation is successful.

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 file changes

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:

After reading the above, do you have any further understanding of how to monitor file changes in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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