Reputation: 632
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)
Upvotes: 7
Views: 11419
Reputation: 7091
Another option is to use the copy_function
argument of copytree
. The advantage is that it will be called for every file that is copied instead of each folder.
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)
Upvotes: 12
Reputation: 4928
Python 2.7
The source code for shutil
from Python 2.7 can be found here. You should be able to copy the source code, then add print name
to line 282.
Python 3.4
The source code for shutil
from Python 3.4 can be found here. You should be able to copy the source code, then add print name
to line 307.
Upvotes: 3
Reputation: 3503
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)
Upvotes: 16