Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python shutil.copytree() is there away to track the status of the copying

Tags:

python

copy

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)
like image 684
TsvGis Avatar asked Oct 21 '14 21:10

TsvGis


People also ask

What is the difference between Shutil copy () and Shutil Copytree ()?

While shutil. copy() will copy a single file, shutil. copytree() will copy an entire folder and every folder and file contained in it.

How does Shutil Copytree work?

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.

What is Copytree?

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.

What is the use of Rmtree ()?

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.


2 Answers

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)
like image 93
user3885927 Avatar answered Sep 25 '22 15:09

user3885927


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)
like image 21
nbeuchat Avatar answered Sep 23 '22 15:09

nbeuchat