为什么shutil.copy2比cp -p慢得多?

时间:2014-05-11 16:09:17

标签: python shutil

我正在将一大堆文件从一个地方移动到另一个地方,其中一些是相当大的.wav文件,并且在目的地的目录结构周围进行更改,因此我无法复制目录。我最初使用的是copyFile函数:http://blogs.blumetech.com/blumetechs-tech-blog/2011/05/faster-python-file-copy.html

def copyFile(src, dst, buffer_size=10485760, perserveFileDate=True):
    '''
    Copies a file to a new location. Much faster performance than Apache Commons due to use of larger buffer
    @param src:    Source File
    @param dst:    Destination File (not file path)
    @param buffer_size:    Buffer size to use during copy
    @param perserveFileDate:    Preserve the original file date
    '''
    #    Check to make sure destination directory exists. If it doesn't create the directory
    dstParent, dstFileName = os.path.split(dst)
    if(not(os.path.exists(dstParent))):
        os.makedirs(dstParent)

    #    Optimize the buffer for small files
    buffer_size = min(buffer_size,os.path.getsize(src))
    if(buffer_size == 0):
        buffer_size = 1024

    if shutil._samefile(src, dst):
        raise shutil.Error("`%s` and `%s` are the same file" % (src, dst))
    for fn in [src, dst]:
        try:
            st = os.stat(fn)
        except OSError:
            # File most likely does not exist
            pass
        else:
            # XXX What about other special files? (sockets, devices...)
            if shutil.stat.S_ISFIFO(st.st_mode):
                raise shutil.SpecialFileError("`%s` is a named pipe" % fn)
    with open(src, 'rb') as fsrc:
        with open(dst, 'wb') as fdst:
            shutil.copyfileobj(fsrc, fdst, buffer_size)

    if(perserveFileDate):
        shutil.copystat(src, dst)

这仍然是永远的,所以只是尝试,我用它替换它:

def copyFile(src, dst):
    os.system('cp -p %s %s' % (src, dst))

我加速了大约10倍!第一个版本是大约22~25 KBps的复制,第二个版本现在是大约220 KBps。这对我自己来说是一个很好的破解,但是我想更好地理解为什么我需要开发&将来会像这样分享代码。

0 个答案:

没有答案