copytree if directory exists

Peter Hansen peter at engcorp.com
Thu Nov 25 16:44:03 EST 2004


Thomas Guettler wrote:
> I need to copy a directory into an existing directory.
> shutil.copytree does complain that the directory already
> exists.
> 
> I need something like "cp -rp": Copy recursively, overwrite
> if file already exist. Preserve mtime and modes if possible.
> 
> A keyword argument "may_exist=1" for shutil.copytree would be nice.

Thomas, shutil.copytree is written in pure Python.  Grab the
source and stick in the appropriate exception handling around
the call to os.mkdir() and you should have about what you
need...

-Peter



More information about the Python-list mailing list