加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

使用Windows复制对话框复制

发布时间:2020-12-14 04:03:54 所属栏目:Windows 来源:网络整理
导读:我目前正在使用shutil.copy2()来复制大量的图像文件和文件夹(0.5到5演出之间的任何地方). Shutil工作正常,但速度很慢.我想知道是否有办法将此信息传递给 Windows来制作副本并给我标准传输对话框.你知道,这家伙…… 很多时候,我的脚本将花费大约两倍的标准Win
我目前正在使用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 = ''.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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读