Qt或PyQt - 检查文件是否被另一个进程使用。等待直到完成复制 [英] Qt or PyQt - check when file is used by another process. Wait until finish copy

查看:506
本文介绍了Qt或PyQt - 检查文件是否被另一个进程使用。等待直到完成复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,
当一个大文件大目录已经完成复制时,检查的最佳策略是什么?
我想等待一个文件完全完成复制。在q中有代码示例

Good morning, What is the best strategy for check when a big file o big directory has finished to copy? I want wait until a file has finish fully to copy. Is there a code example in q

我正在使用mac os x。

I'm working on mac os x.

感谢

更新

我使用QFileSystemWatcher。问题是我收到文件或目录更改通知,当o复制它正在进行中。所以用户复制一个大文件夹(里面很多文件),操作系统复制过程开始,它需要5分钟,但在同一时间我的应用程序接收文件更改通知。这是一个问题,因为当我收到一个更改通知我的应用程序开始对该文件进行一些操作,但该副本已经在进行!!!!

I use QFileSystemWatcher. the problem is that I receive file or directory change notification when o copy it is in progress. So user copy a big folder (inside many files), the operating system copy process start, it take 5 minuts, but in same times my application receive file changed notification. This is a problem because when i receive a change notification my application start for doing some operations on that files, but the copy is already in progress!!!!

推荐答案

我认为QFileSystemWatcher是一个正确的开始,你到达监视的变化的点,但正如你已经发现,这些更改是任何更改。从这一点,我认为应该很容易让你只需检查文件的修改时间。

I think that the QFileSystemWatcher is the right start for you to get to the point of monitoring for changes, but as you have found, these changes are ANY changes. From this point, I think it should be easy enough for you to just check the modification time of the file.

下面是一个Watcher类的简单示例,它允许您指定要监视的文件,并查看在给定时间后是否已修改。它可以运行回调或发出任何人都可以观看的信号:

Here is a simple example of a Watcher class that will let you specify a file to monitor and see if it has been modified after a given time. It can run a callback or emit a signal that anyone can watch:

import os.path
import time

from PyQt4 import QtCore

class Watcher(QtCore.QObject):

    fileNotModified = QtCore.pyqtSignal(str)

    MOD_TIME_DIFF = 5 #seconds

    def __init__(self, aFile, callback=None, checkEvery=5):
        super(Watcher, self).__init__()

        self.file = aFile
        self.callback = callback

        self._timer = QtCore.QTimer(self)
        self._timer.setInterval(checkEvery*1000)
        self._timer.timeout.connect(self._checkFile)

    def _checkFile(self):
        diff = time.time() - os.path.getmtime(self.file)
        if diff > self.MOD_TIME_DIFF:
            self._timer.stop()

            self.fileNotModified.emit(self.file)

            if self.callback:
                self.callback()

    def start(self):
        self._timer.start()

    def stop(self):
        self._timer.stop()

使用它的例子:

def callbackNotify():
    print "Callback!"

def signalNotify(f):
    print "Signal: %s was modified!" % f

# You could directly give it a callback
watcher = Watcher("/path/to/file.file", callback=callbackNotify)
# Or could use a signal
watcher.fileNotModified.connect(signalNotify)
# tell the watcher timer to start checking
watcher.start()

## after the file hasnt been modified in 5 seconds  ##
# Signal: /path/to/file.file was modified!
# Callback!

这篇关于Qt或PyQt - 检查文件是否被另一个进程使用。等待直到完成复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆