Copy using the Windows copy dialog

后端 未结 4 2383
孤城傲影
孤城傲影 2021-02-14 23:47

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

4条回答
  •  耶瑟儿~
    2021-02-15 00:17

    *bump* Windows 10!

    With all your help and Virgil Dupras' send2trash:
    I just cooked a vanilla Python version only using ctypes:

    import os
    import ctypes
    from ctypes import wintypes
    
    
    class _SHFILEOPSTRUCTW(ctypes.Structure):
        _fields_ = [("hwnd", wintypes.HWND),
                    ("wFunc", wintypes.UINT),
                    ("pFrom", wintypes.LPCWSTR),
                    ("pTo", wintypes.LPCWSTR),
                    ("fFlags", ctypes.c_uint),
                    ("fAnyOperationsAborted", wintypes.BOOL),
                    ("hNameMappings", ctypes.c_uint),
                    ("lpszProgressTitle", wintypes.LPCWSTR)]
    
    
    def win_shell_copy(src, dst):
        """
        :param str src: Source path to copy from. Must exist!
        :param str dst: Destination path to copy to. Will be created on demand.
        :return: Success of the operation. False means is was aborted!
        :rtype: bool
        """
        if not os.path.exist(src):
            print('No such source "%s"' % src)
            return False
    
        src_buffer = ctypes.create_unicode_buffer(src, len(src) + 2)
        dst_buffer = ctypes.create_unicode_buffer(dst, len(dst) + 2)
    
        fileop = _SHFILEOPSTRUCTW()
        fileop.hwnd = 0
        fileop.wFunc = 2  # FO_COPY
        fileop.pFrom = wintypes.LPCWSTR(ctypes.addressof(src_buffer))
        fileop.pTo = wintypes.LPCWSTR(ctypes.addressof(dst_buffer))
        fileop.fFlags = 512  # FOF_NOCONFIRMMKDIR
        fileop.fAnyOperationsAborted = 0
        fileop.hNameMappings = 0
        fileop.lpszProgressTitle = None
    
        result = ctypes.windll.shell32.SHFileOperationW(ctypes.byref(fileop))
        return not result
    

    ✔ Tested on Python 3.7 and 2.7 also with long src and dst paths.

提交回复
热议问题