我目前正在使用shutil.copy2()来复制大量的图像文件和文件夹(0.5到5演出之间的任何地方). Shutil工作正常,但速度很慢.我想知道是否有办法将此信息传递给
Windows来制作副本并给我标准传输对话框.你知道,这家伙……
很多时候,我的脚本将花费大约两倍的标准Windows副本所花费的时间,并且让我感到紧张的是我的python解释器在运行副本时挂起.我多次运行复制过程,我希望减少时间.
如果你的目标是一个花哨的复制对话框,SHFileOperation Windows API函数提供了.
pywin32包有一个python绑定,ctypes也是一个选项(例如google“SHFileOperation ctypes”).
原文链接:https://www.f2er.com/windows/365061.htmlpywin32包有一个python绑定,ctypes也是一个选项(例如google“SHFileOperation ctypes”).
这是我使用pywin32的(非常轻微测试的)示例:
import os.path from win32com.shell import shell,shellcon def win32_shellcopy(src,dest): """ Copy files and directories using Windows shell. :param src: Path or a list of paths to copy. Filename portion of a path (but not directory portion) can contain wildcards ``*`` and ``?``. :param dst: destination directory. :returns: ``True`` if the operation completed successfully,``False`` if it was aborted by user (completed partially). :raises: ``WindowsError`` if anything went wrong. Typically,when source file was not found. .. seealso: `SHFileperation on MSDN <http://msdn.microsoft.com/en-us/library/windows/desktop/bb762164(v=vs.85).aspx>` """ if isinstance(src,basestring): # in Py3 replace basestring with str src = os.path.abspath(src) else: # iterable src = '\0'.join(os.path.abspath(path) for path in src) result,aborted = shell.SHFileOperation(( 0,shellcon.FO_COPY,src,os.path.abspath(dest),shellcon.FOF_NOCONFIRMMKDIR,# flags None,None)) if not aborted and result != 0: # Note: raising a WindowsError with correct error code is quite # difficult due to SHFileOperation historical idiosyncrasies. # Therefore we simply pass a message. raise WindowsError('SHFileOperation Failed: 0x%08x' % result) return not aborted
如果将上面的标志设置为shellcon.FOF_SILENT,您也可以在“静默模式”(无对话框,无确认,没有错误弹出窗口)中执行相同的复制操作. shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR.详情请见SHFILEOPSTRUCT.