Issue 1540112: Allow choice of copy function in shutil.copytree (original) (raw)
It would be nice to be able to choose which of the copy functions is used in copytree. I am currently working on a project where I would like to copy a tree, but I do not want to preserve permissions so I'd like copytree to use copyfile instead of copy2.
Here is a snippet of copytree from shutil.py that I modified by adding a func parameter and defaulting it to copy2.
def copytree(src, dst, symlinks=False, func=copy2): . . . try: if symlinks and os.path.islink(srcname): linkto = os.readlink(srcname) os.symlink(linkto, dstname) elif os.path.isdir(srcname): copytree(srcname, dstname, symlinks) else: func(srcname, dstname) . . .
Logged In: YES user_id=1575524
I guess I should have tested it first. I forgot to add the func parameter to the copytree call within copytree.
Something like this:
.try: . if symlinks and os.path.islink(srcname): . linkto = os.readlink(srcname) . os.symlink(linkto, dstname) . elif os.path.isdir(srcname): . copytree(srcname, dstname, symlinks, func) . else: . func(srcname, dstname)