Python automatic monitoring directory file based on watchdog Library

Time:2021-10-11

Wedge

Sometimes we need to monitor a directory to detect whether there are new and deleted files in it, and whether the content of each file has changed. What would you do if it were you?

Obviously, it’s also a troublesome job. It’s not difficult, but it’s more complicated. Fortunately, a third-party package watchdog has helped us achieve this perfectly, so this is python. There are ready-made packages for everything you want to do.

Then let’s take a look at its usage. Of course, it should be installed first. Direct:pip install watchdogJust.

usage method

There is an empty directory test on my desktop, and our operations on this directory will be reflected later. In addition, this package can be adapted to all mainstream operating systems.

?
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
#Import observer and filesystemeventhandler
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
 
 
#Define a class that inherits from filesystemeventhandler
class CodeEventHandler(FileSystemEventHandler):
 
 def on_moved(self, event):
  "" "when a file or directory is renamed" ""
  print(f"{event. Src_path} was renamed to {event. Dst_path}")
 
 def on_created(self, event):
  "" "when a file or directory is created" ""
  print(f"{event. Src_path} was created")
 
 def on_deleted(self, event):
  "" "when a file or directory is deleted" ""
  print(f"{event. Src_path} was deleted")
 
 def on_modified(self, event):
  "" "when a file or directory is modified" ""
  print(f"{event. Src_path} was modified")
 
 
def main():
 #Create an observer
 observer = Observer()
 #Start scheduling and receive three parameters: handler, monitored directory and whether to recurse all subdirectories
 observer.schedule(CodeEventHandler(), r"C:\Users\satori\Desktop\test", True)
 #Start listening. Note: a new daemon thread is started here, so if the program ends, the thread will also stop
 #This also meets our needs
 observer.start()
 
 
main()
while True:
 pass

Let’s demonstrate:

Create a file

?
1
C:\Users\satori\Desktop\test\The new text document. TXT is created

Create a directory

?
1
C:\Users\satori\Desktop\test\The new folder was created

rename

?
1
2
C:\Users\satori\Desktop\test\The new text document. TXT is renamed C: \ users \ satori \ desktop\test\1.txt
C:\Users\satori\Desktop\test\The new folder is renamed C: \ users \ satori \ desktop\test\test_child

Move the file 1.txt to the subdirectory test_ Child, so there will be a creation, a deletion and a modification

?
1
2
3
4
5
C:\Users\satori\Desktop\test\1.txt was deleted//Because the file does not exist after being moved, it is deleted
C:\Users\satori\Desktop\test\test_ Child \ 1.txt was created//Obviously, this file is equivalent to being created
C:\Users\satori\Desktop\test\test_ Child has been modified
//becausetestThe files in the directory have changed, so it is equivalent to being modified
//If it is text, it will monitor whether the content changes, and the directory will monitor whether the number and location of internal files change

Modify file 1.txt

?
1
C:\Users\satori\Desktop\test\1.txt has been modified

Summary

The use method is very simple. It is encapsulated internally for you, so the watchdog package can really save a lot of things.

The above is the details of the python watchdog library based full automatic monitoring directory file. For more information about the python watchdog library full automatic monitoring directory file, please pay attention to other relevant articles of developeppaper!