Python copy larger file too slow

后端 未结 2 639
后悔当初
后悔当初 2020-12-09 03:53

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:-

i         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 04:16

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

    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)
    

    See also:

    • 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

提交回复
热议问题