基於python的文件監控watchdog


  實時監控第三方庫watchdog,其原理通過操作系統的時間觸發的,不需要循環和等待

使用場景:

  1.監控文件系統中文件或目錄的增刪改情況

  2.當特定的文件被創建,刪除,修改,移動時執行相應的任務

 

1. 安裝

pip install watchdog

 

示例:

import re
import os
import logging
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

LUA_FILE_NAME = 'version_info.lua'  #Lua版本文件
WHITELIST_FILE_NAME = 'whitelist.txt'  # 白名單文件

class FileMonitorHandler(FileSystemEventHandler):
    def __init__(self, **kwargs):
        super(FileMonitorHandler, self).__init__(**kwargs)
        # 監控目錄 目錄下面以device_id為目錄存放各自的圖片
        self._watch_path = game_path

    # 重寫文件改變函數,文件改變都會觸發文件夾變化
    def on_modified(self, event):
        if not event.is_directory:  # 文件改變都會觸發文件夾變化
            file_path = event.src_path
            logging.info("file changed: %s " % file_path)
            file_name = os.path.split(file_path)[-1]
            # 白名單或者配置文件修改,則觸發事件
            if file_name == LUA_FILE_NAME:  # lua文件發生變化
                # 驗證該目錄下是否存在白名單文件
                whitelist_file, is_exists = check_file_exists(file_path, WHITELIST_FILE_NAME)
                if not is_exists:  # 不存在白名單,則不進行修改操作
                    logging.info(f'{whitelist_file} not exists')
                else:
                    # 讀取文件進行替換操作
                    whitelist_handler(file_path, whitelist_file)
            elif file_name == WHITELIST_FILE_NAME:  # 白名單文件發生變化
                # 驗證配置文件是否存在
                lua_file, is_exists = check_file_exists(file_path, LUA_FILE_NAME)
                if not is_exists:  # 不存在Lua文件
                    logging.info(f'{lua_file} not exists')
                else:
                    whitelist_handler(lua_file, file_path)
        else:
            logging.info('Director changed')

    def on_created(self, event):
        pass


def check_file_exists(path, file_name):
    """
    驗證文件的存在性
    """
    file_path = os.path.join(os.path.dirname(path), file_name)
    is_exists = os.path.isfile(file_path)
    return file_path, is_exists

def replace_content(lua_file, new_str):
    with open(lua_file, 'r', encoding='utf-8') as f1, open("%s.bak" % lua_file, "w", encoding='utf-8') as f2:
        old_content = f1.read()
        if 'testId' in old_content:
            # 進行正則匹配
            pattern = re.compile(r'testId = "(.*?)"', re.M | re.S)
            new_content = re.sub(pattern, f'testId = "{new_str}"', old_content)
            logging.info('Old content:%s' % old_content)
            logging.info('New content:%s' % new_content)
            f2.write(new_content)

    os.remove(lua_file)
    os.rename("%s.bak" % lua_file, lua_file)


def whitelist_handler(lua_file, whitelist_file):
    """
    白名單處理
    """
    with open(whitelist_file, 'r', encoding='utf-8') as f:
        whitelist_content = f.read().replace("\n", "")
    logging.info(f'Replace content: lua_file->{lua_file} whitelist content->{whitelist_content}')
    replace_content(lua_file, whitelist_content)

def main():
    event_handler = FileMonitorHandler()
    observer = Observer()
    observer.schedule(event_handler, path=game_path, recursive=True)  # recursive遞歸的
    observer.start()
    observer.join()


if __name__ == '__main__':
    global game_path
    game_path = '/test2/files'
    main()

 

 

常見問題:

  1. OSError: inotify watch limit reached 錯誤

  在添加監控任務之后, 發現出現這個錯誤, 是因為已經達到了inotify監控的一個上限值

  查看目前的上限值:

cat /proc/sys/fs/inotify/max_user_watches
 默認是8192

 

  解決:

1)臨時生效
echo 81920 > /proc/sys/fs/inotify/max_user_watches

2)永久
在 /etc/sysctl.conf 中添加一行:
fs.inotify.max_user_watches=99999999
修改后保存
 立即生效    sysctl -p

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM