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

使用Windows复制对话框复制

我目前正在使用shutil.copy2()来复制大量的图像文件文件夹(0.5到5演出之间的任何地方). Shutil工作正常,但速度很慢.我想知道是否有办法将此信息传递给 Windows来制作副本并给我标准传输对话框.你知道,这家伙……

很多时候,我的脚本将花费大约两倍的标准Windows副本所花费的时间,并且让我感到紧张的是我的python解释器在运行副本时挂起.我多次运行复制过程,我希望减少时间.

如果你的目标是一个花哨的复制对话框,SHFileOperation Windows API函数提供了.
pywin32包有一个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.

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

相关推荐