使用 Windows 复制对话框复制 [英] Copy using the Windows copy dialog

查看:104
本文介绍了使用 Windows 复制对话框复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently using shutil.copy2() to copy a large number of image files and folders (anywhere between 0.5 and 5 gigs). Shutil works fine, but it's so slow. I'm wondering if there is a way to pass this info over to Windows to make the copy and give me its standard transfer dialog box. You know, this guy...

很多时候,我的脚本需要的时间大约是标准 Windows 副本的两倍,这让我担心我的 Python 解释器在运行副本时挂起.我多次运行复制过程,希望缩短时间.

Many times, my script will take about twice the time the standard windows copy takes, and it makes me nervous that my python interpreter hangs while running the copy. I run the copy process multiple times and I'm looking to cut the time down.

推荐答案

如果您的目标是精美的复制对话框,SHFileOperation Windows API 函数提供了这一点.pywin32 包有一个 python 绑定,ctypes 也是一个选项(例如谷歌SHFileOperation ctypes").

If your goal is a fancy copy dialog, SHFileOperation Windows API function provides that. pywin32 package has a python binding for it, ctypes is also an option (google "SHFileOperation ctypes" for examples).

这是我使用 pywin32 的(非常简单的测试)示例:

Here is my (very lightly tested) example using 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 了解详情.

You can also perform the same copy operation in "silent mode" (no dialog, no confirmationsm, no error popups) if you set the flags above to shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR. See SHFILEOPSTRUCT for details.

这篇关于使用 Windows 复制对话框复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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