我有很多光栅文件(600+)在目录中,我需要复制到一个新的位置(包括它们的目录结构)。是否有一种使用shutil.copytree()跟踪复制状态的方法?通常,对于文件,我将使用下面的代码,但不确定如何对shutil.copytree()执行相同的操作:
for currentFolder, subFolder, fileNames in os.walk(sourceFolder):
for i in fileNames:
if i.endswith(".img"):
print "copying {}".format(i)
shutil.copy(os.path.join(currentFolder,i), outPutFolder)发布于 2014-10-21 21:56:58
是的,通过利用传入的“忽略”参数的函数名,类似的事情是可能的。实际上,类似的内容在python:https://docs.python.org/2/library/shutil.html#copytree-example的示例部分中给出了。
下面还粘贴了示例:
from shutil import copytree
import logging
def _logpath(path, names):
logging.info('Working in %s' % path)
return [] # nothing will be ignored
copytree(source, destination, ignore=_logpath)发布于 2017-02-06 13:27:37
另一个选项是使用copy_function参数copytree。它的优点是,它将被调用的每一个文件被复制,而不是每个文件夹。
from shutil import copytree,copy2
def copy2_verbose(src, dst):
print('Copying {0}'.format(src))
copy2(src,dst)
copytree(source, destination, copy_function=copy2_verbose)https://stackoverflow.com/questions/26496821
复制相似问题