我把一大堆文件从一个地方移到另一个地方,其中一些相当大的.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)
这还需要很长时间,所以我只是做了一个实验,我把它换成了:
^{pr2}$我最终得到了10倍的加速!第一个版本的复制速度约为22~25kbps,而第二个版本的复制速度约为220kbps。这对我自己来说是一个不错的方法,但是我想更好地理解为什么将来我需要开发和共享这样的代码。在
目前没有回答
相关问题 更多 >
编程相关推荐