copytree if directory exists

Thomas Heller theller at python.net
Fri Nov 26 02:52:58 EST 2004


Peter Hansen <peter at engcorp.com> writes:

> 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...

Also, distutils.dir_util has a fairly flexible copy_tree function.

Thomas



More information about the Python-list mailing list