tool_watch_file

import os
import shutil
import time

watched_path = '/data/tmp/'
#watched_path = '/tmp/'
target_dir = '/tmp/guyu/'

watched_file = 'index-d-'
watched_files = {}

for file in os.listdir(watched_path):
    if file.startswith(watched_file):
        watched_files[file] = None

for k in watched_files.keys():
    watched_files[k] = os.path.getmtime(''.join([watched_path, k]))

while True:

    time.sleep(60)
    print('check files')

    saved_files = set(watched_files.keys())
    current_files = []
    for file in os.listdir(watched_path):
        if file.startswith(watched_file):
            current_files.append(file)

    erase = list(saved_files - set(current_files))
    for file in erase:
        if file in watched_files:
            print('delete',file)
            del watched_files[file]

    diff = list(set(current_files) - saved_files)
    if diff:
        print('diff =',diff)
        for file in diff:
            watched_files[file] = 0

    try:
        for file in watched_files.keys():
            current_modified_time = os.path.getmtime(''.join([watched_path, file]))

            #print('modifytime',current_modified_time, watched_files[file])
            test_time = current_modified_time - watched_files[file]

            if current_modified_time - watched_files[file] > 0.1:
                target_file = os.path.join(target_dir, '{}_{}'.format(file, time.strftime('%Y%m%d%H%M%S')))
                shutil.copy2(''.join([watched_path, file]), target_file)
                watched_files[file] = current_modified_time
                print('copy', target_file)

    except Exception as e:
        print(e)
posted @   小葱饼子  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示