本文介绍了Python shutil.copytree()可以跟踪复制状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录中有很多光栅文件(600+),需要将其复制到新位置(包括其目录结构)。有没有一种方法可以使用shutil.copytree()来跟踪复制状态?通常对于文件,我将使用以下代码,但不确定如何使用shutil.copytree():

  os.walk(sourceFolder)中的currentFolder,subFolder,fileName:文件名中i的
:如果i.endswith(。img):

打印 copying {}。format( i)
shutil.copy(os.path.join(currentFolder,i),outPutFolder)




下面也粘贴了示例:

 从shutil导入copytree 
导入日志

def _logpath(路径,名称):
logging.info('正在%s'%路径中工作)
return []#不会被忽略

copytree(源,目标,ignore = _logpath)


I have a lot of raster files (600+) in directories that I need copy into a new location (including their directory structure). Is there a way to track the status of the copying using shutil.copytree()? Normally with files I would use the code below, but not sure how to do the same with 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)
解决方案

Yes, something like this is possible by taking advantage of the function name passed in for 'ignore' parameter. In fact something like this is given in the example section of python docs:https://docs.python.org/2/library/shutil.html#copytree-example

Example pasted below as well:

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)

这篇关于Python shutil.copytree()可以跟踪复制状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 10:54