python based on watchdog library fully automatic monitoring directory files

  • 2021-09-20 21:06:15
  • OfStack

Catalog wedge
Usage
Create 1 file Create 1 directory Rename Moves file 1. txt to subdirectory test_child, so the file 1. txt summary is modified with 1 creation, 1 deletion, and 1 modification

Wedge

Sometimes we need to monitor a directory to detect whether there are new or deleted files in it, and whether the contents of each file have changed. If it were you at this time, what would you choose to do?

Obviously, it is also a troublesome job, not that it is difficult, but mainly complicated. Fortunately, there is already a third-party package watchdog that helps us achieve this point perfectly, so this is Python, and everything we want to do is ready-made.

Then let's look at its usage. Of course, it must be installed first. Direct: pip install watchdog That's enough.

Usage

There is an empty directory test on my desktop, which will be reflected in the operations we do on this directory later. In addition, this package can adapt to all mainstream operating systems.


#  Import Observer And FileSystemEventHandler
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


#  Definition 1 Category ,  Inherit from FileSystemEventHandler
class CodeEventHandler(FileSystemEventHandler):

 def on_moved(self, event):
  """ When 1 When a file or directory is renamed """
  print(f"{event.src_path}  Was renamed to  {event.dst_path}")

 def on_created(self, event):
  """ When 1 When a file or directory is created """
  print(f"{event.src_path} Was created ")

 def on_deleted(self, event):
  """ When 1 When a file or directory is deleted """
  print(f"{event.src_path} Has been deleted ")

 def on_modified(self, event):
  """ When 1 When a file or directory is modified """
  print(f"{event.src_path} Be modified ")


def main():
 #  Create 1 Observers 
 observer = Observer()
 #  Start scheduling ,  Receive 3 Parameters : handler Is all subdirectories recursive 
 observer.schedule(CodeEventHandler(), r"C:\Users\satori\Desktop\test", True)
 #  Start listening ,  Attention :  Here is the opening 1 New daemon threads ,  So if the program ends, ,  The thread also stops 
 #  This is also in line with our needs 
 observer.start()


main()
while True:
 pass

Let's demonstrate 1:

Create 1 file


C:\Users\satori\Desktop\test\ New Text Document .txt Was created 

Create 1 directory


C:\Users\satori\Desktop\test\ New Folder   Was created 

Rename


C:\Users\satori\Desktop\test\ New Text Document .txt  Was renamed to  C:\Users\satori\Desktop\test\1.txt
C:\Users\satori\Desktop\test\ New Folder   Was renamed to  C:\Users\satori\Desktop\test\test_child

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


C:\Users\satori\Desktop\test\1.txt  Has been deleted  // Because the file does not exist after it is moved ,  So it was deleted 
C:\Users\satori\Desktop\test\test_child\1.txt  Was created  // Obviously, at this time, the file is equivalent to being created 
C:\Users\satori\Desktop\test\test_child  Be modified  
// Because test The files in the directory have changed ,  Therefore, it is equivalent to being modified 
// If it is text ,  It will monitor whether the content changes ,  The directory monitors whether the number and location of internal files change 

Modify file 1. txt


C:\Users\satori\Desktop\test\1.txt  Be modified 

Summary

The use method is very simple, and the interior is packaged for you, so watchdog can really save a lot of things.

The above is python based on watchdog library fully automated monitoring directory file details, more information about python watchdog library fully automated monitoring directory file please pay attention to other related articles on this site!


Related articles: