我目前使用shutil.copy2()
來複制大量的圖像文件和文件夾(任何地方在0.5到5個演出之間)。 Shutil
工作正常,但速度很慢。我想知道是否有辦法將這些信息傳遞給Windows來製作副本並給我它的標準傳輸對話框。要知道,這傢伙......使用Windows複製對話框複製
很多時候,我的腳本將需要大約兩倍的標準的windows複製所需的時間,這讓我很緊張,我的Python解釋器在運行復制掛起。我多次運行復制過程,並且期望縮短時間。
我目前使用shutil.copy2()
來複制大量的圖像文件和文件夾(任何地方在0.5到5個演出之間)。 Shutil
工作正常,但速度很慢。我想知道是否有辦法將這些信息傳遞給Windows來製作副本並給我它的標準傳輸對話框。要知道,這傢伙......使用Windows複製對話框複製
很多時候,我的腳本將需要大約兩倍的標準的windows複製所需的時間,這讓我很緊張,我的Python解釋器在運行復制掛起。我多次運行復制過程,並且期望縮短時間。
請參閱IFileCopy。 IFileOperation 可能可通過ctypes和shell32.dll,我不知道。
如果你的目標是一個奇特的複製對話框,SHFileOperation Windows API函數提供。 pywin32包有一個Python綁定它,ctypes也是一個選項(谷歌「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
您還可以在「靜音模式」(無對話,無confirmationsm,沒有錯誤彈出窗口),如果你設置執行相同的複製操作上面的標誌爲shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR.
詳見SHFILEOPSTRUCT。
我在這裏詢問了關於_delete_的'shell.SHFileOperation'的返回值:http://stackoverflow.com/questions/29053189/python-win32com-shell-shfileoperation-any-way-to-get-the-文件 - 是 - 是 - ACTUA。似乎無法找到任何文檔 –
在這裏,我再次想知道'src = os.path.abspath(src)'應該是'src = os.path.abspath(src)+'\ 0''(與下面的連接) –
更新:見
將是很好,有它包裹在一個圖書館......隨着答案的幫助上面,我是能夠得到它在Windows 7上工作如下。這裏
import pythoncom
from win32com.shell import shell,shellcon
def win_copy_files(src_files,dst_folder):
# @see IFileOperation
pfo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation,None,pythoncom.CLSCTX_ALL,shell.IID_IFileOperation)
# Respond with Yes to All for any dialog
# @see http://msdn.microsoft.com/en-us/library/bb775799(v=vs.85).aspx
pfo.SetOperationFlags(shellcon.FOF_NOCONFIRMATION)
# Set the destionation folder
dst = shell.SHCreateItemFromParsingName(dst_folder,None,shell.IID_IShellItem)
for f in src_files:
src = shell.SHCreateItemFromParsingName(f,None,shell.IID_IShellItem)
pfo.CopyItem(src,dst) # Schedule an operation to be performed
# @see http://msdn.microsoft.com/en-us/library/bb775780(v=vs.85).aspx
success = pfo.PerformOperations()
# @see sdn.microsoft.com/en-us/library/bb775769(v=vs.85).aspx
aborted = pfo.GetAnyOperationsAborted()
return success and not aborted
files_to_copy = [r'C:\Users\jrm\Documents\test1.txt',r'C:\Users\jrm\Documents\test2.txt']
dest_folder = r'C:\Users\jrm\Documents\dst'
win_copy_files(files_to_copy,dest_folder)
的引用也非常有幫助: http://timgolden.me.uk/pywin32-docs/html/com/win32com/HTML/QuickStartClientCom.html
你有沒有實際使用定時Python和Windows資源管理器相同的文件的文件傳輸?我很難相信Python實際上比較慢。 –
是的,我做了一個並排測試。它是通過網絡傳輸的,因此網絡速度可能會受到干擾,但是如何通過shutil找到我的傳輸速度? – tylerART
你可以在Python中使用'time.clock()'來獲得傳輸時間,但是你必須使用秒錶來定時瀏覽器。我的假設是,Python和Explorer都會執行相同的庫調用來執行副本,但由於進度條的原因,Explorer可能感覺更快,也可能是因爲它提供了一些不正確的時間估計值。如果你同時跑步並看到巨大差異,那很有趣! –