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)
While shutil. copy() will copy a single file, shutil. copytree() will copy an entire folder and every folder and file contained in it.
shutil. copytree() method recursively copies an entire directory tree rooted at source (src) to the destination directory. The destination directory, named by (dst) must not already exist. It will be created during copying.
copytree is a REXX sample that enables you to use a number of z/OS® UNIX capabilities. Included is a recursive routine to descend a hierarchical directory. You can also use it to accomplish the following tasks: Retrieve and set attributes for files. Read and write files.
rmtree() is used to delete an entire directory tree, path must point to a directory (but not a symbolic link to a directory). Parameters: path: A path-like object representing a file path.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With