I wrote a simple recursive function in pure python that merge a folder and its content into an other:
import os
import shutil
def merge(scr_path, dir_path):
files = next(os.walk(scr_path))[2]
folders = next(os.walk(scr_path))[1]
for file in files: # Copy the files
scr_file = scr_path + "/" + file
dir_file = dir_path + "/" + file
if os.path.exists(dir_file): # Delete the old files if already exist
os.remove(dir_file)
shutil.copy(scr_file, dir_file)
for folder in folders: # Merge again with the subdirectories
scr_folder = scr_path + "/" + folder
dir_folder = dir_path + "/" + folder
if not os.path.exists(dir_folder): # Create the subdirectories if dont already exist
os.mkdir(dir_folder)
merge(scr_folder, dir_folder)
path1 = "path/to/folder1"
path2 = "path/to/folder2"
merge(path1, path2)
Here the folder1
is merged into folder2
.
For every folder1
elements, it check if the element already exist in the folder2
, if so the element is replaced, if not the element is created.
In the end, the merged folder contains its original elements plus the elements of the other folder.