微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python看门狗窗口等到复制完成

我在 Windows 2012服务器上使用 Python监视程序模块来监视共享驱动器上出现的新文件.当看门狗注意到新文件时,它将启动数据库恢复过程.

但是,监视程序似乎会尝试在创建的第二个文件中恢复文件,而不是等到文件完成复制到共享驱动器.所以我将事件更改为on_modified但是有两个on_modified事件,一个文件最初被复制时的事件,另一个是完成复制后的事件.

如何将两个on_modified事件处理为仅在复制到共享驱动器的文件完成时触发?

将多个文件同时复制到共享驱动器时会发生什么?

这是我的代码

import time
import subprocess
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class NewFile(FileSystemEventHandler):
    def process(self,event):
        if event.is_directory:
            return

    if event.event_type == 'modified':            
        if getext(event.src_path) == 'gz':
            load_pgdump(event.src_path)

    def on_modified(self,event):
        self.process(event)

def getext(filename):
    "Get the file extension"
    file_ext = filename.split(".",1)[1]
    return file_ext

def load_pgdump(src_path):    
    restore = 'pg_restore command ' + src_path
    subprocess.call(restore,shell=True)

def main():
    event_handler = NewFile()
    observer = Observer()
    observer.schedule(event_handler,path='Y:\\',recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

if __name__ == '__main__':
    main()

解决方法

在on_modified事件中,只需等待文件完成复制,即可通过观察文件大小.

提供更简单的循环:

historicalSize = -1
while (historicalSize != os.path.getsize(filename)):
  historicalSize = os.path.getsize(filename)
  time.sleep(1)
print "file copy has Now finished"

原文地址:https://www.jb51.cc/python/242045.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐