I am trying to recursively copy a directory into another directory using shutil.copytree() this works great to a degree.
The problem I run into is that I have multiple directories that I want to copy into the destination directory. It seems that the only option is to overwrite whatever is in the destination directory when I copy the files.
Is there a way of recursively copying the files from one directory into another that does not overwrite the destination? Googling around has only turned up options for overwriting the destination using shutil.copytree()
9
I haven’t tested this, but I think you can take advantage of the copy_function
parameter. From the docs:
If
copy_function
is given, it must be a callable that will be used
to copy each file. It will be called with the source path and the
destination path as arguments. By default,copy2()
is used, but any
function that supports the same signature (likecopy()
) can be used.
So, you could use something like
import os
def copy_if_not_exists(src, dst):
if os.path.exists(dst):
return
shutil.copy2(src, dst)
And then just use:
shutil.copytree(foo, bar, copy_function=copy_if_not_exists)
This may work for simple use-cases. Again, I haven’t tested it.
You can write your own function using os.walk on source directory and write files in the destination directory by replacing the source path with destination in file path.
This will also allow you to have any edge case handling you need like same name files in both directory.
If it’s a large directory with many files you could use ThreadPool or AsyncIO[aiofiles or something similar] to work with reading and writing multiple files
1