如果您的目标是一个精美的复制对话框,SH文件操作 http://msdn.microsoft.com/en-us/library/windows/desktop/bb762164%28v=vs.85%29.aspxWindows 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.
See 文件操作结构 http://msdn.microsoft.com/en-us/library/windows/desktop/bb759795%28v=vs.85%29.aspx了解详情。