Python复制较大的文件太慢 [英] Python copy larger file too slow

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

问题描述

我试图使用 shutil.copy 从硬盘复制一个大文件(> 1 GB)到usb驱动器。一个描述我想要做的简单脚本是: -

I am trying to copy a large file (> 1 GB) from hard disk to usb drive using shutil.copy. A simple script depicting what I am trying to do is:-

import shutil
src_file = "source\to\large\file"
dest = "destination\directory"
shutil.copy(src_file, dest)

在linux上只需要2-3分钟。但同一文件在同一文件复制需要更多的10-15分钟在Windows下。

It takes only 2-3 min on linux. But the same file copy on same file takes more that 10-15 min under Windows. Can somebody explain why and give some solution, preferably using python code?

更新1

将文件另存为test.pySource文件大小为1 GB。指定目录在USB驱动器中。使用ptime计算文件复制时间。结果在这里: -

Saved the file as test.pySource file size is 1 GB. Destinantion directory is in USB drive. Calculated file copy time with ptime. Result is here:-

ptime.exe test.py

ptime 1.0 for Win32, Freeware - http://www.
Copyright(C) 2002, Jem Berkes <jberkes@pc-t

===  test.py ===

Execution time: 542.479 s

542.479 s == 9分钟。我不认为 shutil.copy 需要9分钟才能复制1 GB文件。

542.479 s == 9 min. I don't think shutil.copy should take 9 min for copying 1 GB file.

更新2

Update 2

USB的运行状况良好,同样的脚本在Linux下运行良好。计算时间与windows下的相同文件本机xcopy.Here是结果。

Health of the USB is good as same script works well under Linux. Calculated time with same file under windows native xcopy.Here is the result.

ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>

===  xcopy F:\test.iso L:\usb\test.iso
1 File(s) copied

Execution time: 128.144 s

128.144 s == 2.13分钟。即使复制测试文件后,我仍有1.7 GB的可用空间。

128.144 s == 2.13 min. I have 1.7 GB free space even after copying test file.

推荐答案

您的问题与Python无关。事实上,Windows复制过程与Linux系统相比真的很差。

Your problem has nothing to do with Python. In fact, the Windows copy process is really poor compared to the Linux system.

你可以使用 xcopy robocopy (Ubuntu)Linux文件复制算法比Windows 7好吗?但在这种情况下,你必须对Linux和Windows进行不同的调用...

You can improve this by using xcopy or robocopy according to this post: Is (Ubuntu) Linux file copying algorithm better than Windows 7?. But in this case, you have to make different calls for Linux and Windows...

import os
import shutil
import sys

source = "source\to\large\file"
target = "destination\directory"

if sys.platform == 'win32':
    os.system('xcopy "%s" "%s"' % (source, target))
else:
    shutil.copy(source, target)

另请参阅:

  • Actual Performance, Perceived Performance, Jeff Atwood blog post about this subject
  • Windows xcopy is not working in python, which gives the syntax for using xcopy on Windows in fact

这篇关于Python复制较大的文件太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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