Skip to main content
 首页 » 编程设计

python之使用 Python 在目录中监视文件,然后使用 POST 请求发送文件修改数据

2024年11月01日9www_RR

我想查看两个不同的 excel 文件修改目录(时间戳),修改后我想调用一个 API HTTP Post 请求到一个端点,我已经使用 Python 看门狗和请求库编写了下面的代码,但面临两个错误在相同的。

问题 1:- event(event.event_type == 'modified') 在一个文件修改时触发两次,这导致发送两个 post 数据请求。那么在看门狗库中监视文件修改的正确事件类型是什么,这将使这个条件代码只为真一次。

问题 2:- 在“Watcher”类的“start”函数中,我无法将 Handler() 值分配给 event_handler 变量。我在这里犯了什么错误?

请指导我解决此问题或任何其他更好的方法。提前谢谢你

    import time  
from time import sleep 
from watchdog.observers import Observer 
from watchdog.events import FileSystemEventHandler 
import json 
import requests 
import pandas as pd 
import os 
from collections import defaultdict 
 
class Handler(FileSystemEventHandler): 
 
    def __init__(self, path): 
        super().__init__() 
        self.path = path    
        self.files = defaultdict(lambda:0)      
 
    def on_modified(self, event): 
        if event.is_directory: 
            return None 
 
        elif (event.event_type == 'modified' and event.src_path.endswith('.csv')): 
                # api_url = 'http://10.166.72.3:8080/promo/summary?userId=abc' 
                stats = os.stat(event.src_path).st_ctime 
                sleep(5) 
                if stats - self.files[event.src_path] > 1: 
                    df = pd.read_csv(self.path) 
                    df1 = df.to_json(orient='records') 
                    df1.replace("\\","") 
                    print(df1) 
 
        self.files[event.src_path] = stats 
                #r = requests.post(url=api_url, json=df1) 
                #print(r.status_code, r.reason, r.text) 
 
 
class Watcher: 
 
    def __init__(self, directory, handler): 
        self.directory = directory 
        self.handler = handler 
        self.observer = Observer() 
 
    def start(self): 
        #event_handler = Handler() 
        self.observer.schedule( self.handler, self.directory, recursive=True) 
        self.observer.start() 
 
    def stop(self): 
        self.observer.stop() 
 
    def join(self): 
        self.observer.join() 
 
if __name__ == '__main__':  
 
    handler1 = Handler('C:\\Users\\BPM_admin\\Desktop\\OCR_RPA\\FirstOCR\\Diageo\\Output\\InvoiceMasterData.csv') 
    handler2 = Handler('C:\\Users\\BPM_admin\\Desktop\\OCR_RPA\\SecondOCR\\Diageo\\Output\\AgreementMasterData.csv') 
 
    w1 = Watcher("C:\\Users\\BPM_admin\\Desktop\\OCR_RPA\\FirstOCR\\Diageo\\Output", handler1)  
    w2 = Watcher("C:\\Users\\BPM_admin\\Desktop\\OCR_RPA\\SecondOCR\\Diageo\\Output", handler2)  
 
    w1.start()  
    w2.start()  
 
    try: 
       while True: 
           time.sleep(5) 
    except: 
        w1.stop()  
        w2.stop()  
        print("Error") 
 
    w1.join()  
    w2.join()  

请您参考如下方法:

问题 1:“已修改”事件触发了两次

出现此问题是因为保存文件时可能会发生多个操作,数据已更改,然后是元数据(最后修改...)。如果有很多用户,根据您的需要和更改频率可能很难处理。

首先,您应该通过测试扩展来限制观看的文件,以避免临时文件和所有其他格式。然后我建议保存最后一次修改文件以便能够在两个事件之间进行比较,并且仅当延迟超过 X 秒时才触发 API 调用。

import os 
from collections import defaultdict 
 
class Handler(FileSystemEventHandler): 
 
    def __init__(self): 
        super().__init__() 
        # this dict will store the filenames and the time 
        self.files = defaultdict(lambda:0) 
 
    def on_any_event(self, event): 
        if event.is_directory: 
            return None 
 
        elif (event.event_type == 'modified' and  
              event.src_path.endswith(('.xls','.xlsx'))) : 
 
            # here we get the last change timestamp  
            stats = os.stat(event.src_path).st_mtime 
 
            # delay of 1 sec minimum between 2 changes 
            # self.files[event.src_path] is set to 0 thanks to the defaultdict 
            if stats - self.files[event.src_path] > 1:  
                print('api call with ', event.src_path) 
                # do what you need 
 
            # then update the time for this file 
            self.files[event.src_path] = stats 

问题2:传递handler参数

实例化 Handler() 时出现错误,因为您在构造函数中创建了参数 path :

class Handler(FileSystemEventHandler): 
 
    def __init__(self, path): 
        super().__init__() 
        self.path = path         

你似乎没有在 Handler 中使用它,也许你可以删除这个参数?否则只需提供您要处理的路径,例如:

def start(self): 
    event_handler = Handler(self.directory) # or Handler() if you remove path 
    self.observer.schedule(event_handler, self.directory, recursive=True) 
    self.observer.start()