Python Watchdog process existing files on startup

2024/11/14 15:07:23

I have a simple Watchdog and Queue process to monitor files in a directory. Code taken from https://camcairns.github.io/python/2017/09/06/python_watchdog_jobs_queue.html

import time
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
from queue import Queue
from threading import Threaddir_path = "/data"def process_queue(q):while True:if not q.empty():event = q.get()print("New event %s" % event)time.sleep(5)class FileWatchdog(PatternMatchingEventHandler):def __init__(self, queue, patterns):PatternMatchingEventHandler.__init__(self, patterns=patterns)self.queue = queuedef process(self, event):self.queue.put(event)def on_created(self, event):self.process(event)if __name__ == '__main__':watchdog_queue = Queue()worker = Thread(target=process_queue, args=(watchdog_queue,))worker.setDaemon(True)worker.start()event_handler = FileWatchdog(watchdog_queue, patterns="*.ini")observer = Observer()observer.schedule(event_handler, path=dir_path)observer.start()try:while True:time.sleep(2)except KeyboardInterrupt:observer.stop()observer.join()

Once the process is running new files are processed correctly. However if I restart the process and a file already exists in the directory it is ignored.

I have tried to create a dict to add to the queue

    for file in os.listdir(dir_path):if file.endswith(".ini"):file_path = os.path.join(dir_path, file)event = {'event_type' : 'on_created', 'is_directory' : 'False', 'src_path' : file_path}watchdog_queue.put(event)

but it's expecting an object of type (class 'watchdog.events.FileCreatedEvent') and I can't work out how to create this.

Alternatively I can see in the Watchdog documentation (class watchdog.utils.dirsnapshot.DirectorySnapshot) but I cannot work out how to run this and add it to the queue.

Any suggestions on how I can add existing files to the queue on startup ?

Answer

This code should do what you are trying to achieve.

from watchdog.events import FileCreatedEvent# Loop to get all files; dir_path is your lookup folder.for file in os.listdir(dir_path):filename = os.path.join(dir_path, file)event = FileCreatedEvent(filename)watchdog_queue.put(event)
https://en.xdnf.cn/q/72049.html

Related Q&A

Updating Text In Entry (Tkinter)

The piece of code below takes input from user through a form and then returns the input as multiplied by 2. What I want to do is, when a user types a number (for example 5) and presses the "Enter&…

Python prevent overflow errors while handling large floating point numbers and integers

I am working on a python program to calculate numbers in the Fibonacci sequence. Here is my code:import math def F(n):return ((1+math.sqrt(5))**n-(1-math.sqrt(5))**n)/(2**n*math.sqrt(5)) def fib(n):for…

Python selenium sending keys into textarea

Im using Python 3.4.4 to access a website (https://readability-score.com/) that has a textarea, which dynamically updates when new values are added. Im trying to input a string into that textarea box b…

how to run several executable using python?

I have an executable under linux. I have an 8 core processor. I want to run 8 different instances of the same executable with different arguments.I tried os.system("process_name args")It does…

How to retrieve only arabic texts from a string using regular expression?

I have a string which has both Arabic and English sentences. What I want is to extract Arabic Sentences only.my_string=""" What is the reason ذَلِكَ الْكِتَابُ لَا رَ…

Formatted output in OpenOffice/Microsoft Word with Python

I am working on a project (in Python) that needs formatted, editable output. Since the end-user isnt going to be technically proficient, the output needs to be in a word processor editable format. The …

Issue in calling Python code from Java (without using jython)

I found this as one of the ways to run (using exec() method) python script from java. I have one simple print statement in python file. However, my program is doing nothing when I run it. It neither pr…

AttributeError: tuple object has no attribute dim, when feeding input to Pytorch LSTM network

I am trying to run the following code:import matplotlib.pylab as plt import numpy as np import torch import torch.nn as nnclass LSTM(nn.Module):def __init__(self, input_shape, n_actions):super(LSTM, se…

Python - Idiom to check if string is empty, print default

Im just wondering, is there a Python idiom to check if a string is empty, and then print a default if its is?(The context is Django, for the __unicode__(self) function for UserProfile - basically, I w…

Does WordNet have levels? (NLP)

For example...Chicken is an animal. Burrito is a food.WordNet allows you to do "is-a"...the hiearchy feature.However, how do I know when to stop travelling up the tree? I want a LEVEL. That …