【发布时间】:2017-07-01 09:51:01
【问题描述】:
在 Windows 上,我想使用 Python 通过网络复制一堆文件。有时,网络没有响应,复制停滞不前。我想检查,如果发生这种情况,并在发生这种情况时跳过有问题的文件。通过询问这个相关问题here,我发现了CopyFileEx 函数,它允许使用回调函数,可以中止文件复制。
Python 中的实现如下所示:
import win32file
def Win32_CopyFileEx( ExistingFileName, NewFileName, Canc = False):
win32file.CopyFileEx(
ExistingFileName, # PyUNICODE | File to be copied
NewFileName, # PyUNICODE | Place to which it will be copied
Win32_CopyFileEx_ProgressRoutine, # CopyProgressRoutine | A python function that receives progress updates, can be None
Data = None, # object | An arbitrary object to be passed to the callback function
Cancel = Canc, # boolean | Pass True to cancel a restartable copy that was previously interrupted
CopyFlags = win32file.COPY_FILE_RESTARTABLE, # int | Combination of COPY_FILE_* flags
Transaction = None # PyHANDLE | Handle to a transaction as returned by win32transaction::CreateTransaction
)
从 CopyFileEx 函数的文档中,我可以看到取消正在运行的副本的两种可能性。
pbCancel [in, optional] 如果在复制操作期间将此标志设置为 TRUE,则取消操作。否则,副本 操作将继续完成。
我不知道如何做到这一点。我尝试再次使用相同的文件句柄调用相同的函数,但取消标志设置为TRUE,但这会导致错误,因为有问题的文件正在被另一个进程使用。
另一种可能性似乎是回调函数:
lpProgressRoutine [in, optional] 回调函数的地址 类型 LPPROGRESS_ROUTINE 每次调用的另一部分 该文件已被复制。此参数可以为 NULL。更多 有关进度回调函数的信息,请参阅 CopyProgressRoutine 函数。
documentation of this ProgressRoutine 声明,当开始复制或完成复制垃圾文件时调用此回调。如果回调函数返回1 或2(取消,停止),则回调函数可以取消复制过程。然而,这个回调函数似乎没有被调用,当一个垃圾的副本被停止时。
所以我的问题是:当它停止时,我如何可以按文件取消这个副本?
【问题讨论】:
标签: python windows callback copy timeout