Merging two folders in python.

Recently i wanted to merge two folders in python and overwrite existing files in the destination folders. Shutil library offers a lot of high level file handling functions, however, noone of them offers offer the merging. In particular the function shutil.copytree() copy recursively the source three in the destination tree, but it requires that the destination folder must not exist.

In order to merge folders, here there is a small function that does exactly that:


#recursively merge two folders including subfolders
def mergefolders(root_src_dir, root_dst_dir):
    for src_dir, dirs, files in os.walk(root_src_dir):
        dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1)
        if not os.path.exists(dst_dir):
            os.makedirs(dst_dir)
        for file_ in files:
            src_file = os.path.join(src_dir, file_)
            dst_file = os.path.join(dst_dir, file_)
            if os.path.exists(dst_file):
                os.remove(dst_file)
            shutil.copy(src_file, dst_dir)


Click to rate this post!
[Total: 16 Average: 4.2]

10 Replies to “Merging two folders in python.”

    1. Sure.. os.walk() return a list of sub-directories and files present in a folder. So I do that for the source folder, so i know all the files and directory that i need to copy over .
      Then i check if the destination folder exist.. if not i create it.
      Then i create the full path for the source file and the destination file, by joining the relative directory name and the filename.
      Then i check if the file already exist in the destination directory.. if it exists, i deleted it and replace with the new one, otherwise i copy it directly. And i do this for all the files. I suggest that you read how the os.walk function works because it could be tricky.

  1. Hi.

    This looks close to solving a problem I have.

    I want to merge two folders based on their folder names (if they contain the same date anywhere in their name, they should be merged).

    It would be nice if I could automatically delete the empty one too, but that’s not as important.

    Any thoughts?

    1. PS. I have a large number of folders (thousands) and I want the code to automatically identify and merge duplicates (as described above). Creating a code to just merge two folders I can do. Cheers

      1. source = “C:\\test\\ASource\\”
        dest1 = “C:\\test\\BDest\\”

        shutil.copytree(source,dest1, dirs_exist_ok=True)
        somthing like this?

        1. Just a clarification… As Ben Simpson pointed out, the “dirs_exist_ok” option is only available from python 3.8 and up.

  2. I mean, thats why I became a programmer.
    Everything I need someone else already did for me.
    Thank you for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *