使用Windows复制对话框复制

tyl*_*ART 8 python windows python-2.7

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

http://www.top-windows-tutorials.com/images/file-copy.jpg

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

Nik*_*kin 5

如果您的目标是一个精美的复制对话框,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
Run Code Online (Sandbox Code Playgroud)

shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR.如果您将上面的标志设置为有关详细信息,请参阅SHFILEOPSTRUCT,您还可以在“静默模式”(无对话框、无确认、无错误弹出窗口)下执行相同的复制操作。


kir*_*sos 1

请参阅IFileCopy。IFileOperation可能可以通过 ctypes 和 shell32.dll 获得,我不确定。