4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

Merge 2 directories in python

![17ivwxwo5n38wjpg.jpg](https://cdn.steemitimages.com/DQmUbo8cYS82ApZ6EVuG59PuFayJgiQ9aHVGKRGN2EXQtPB/17ivwxwo5n38wjpg.jpg) Merge 2 folers/directories using python
```py import os import shutil

def move_merge_dirs(source_root, dest_root):
for path, dirs, files in os.walk(source_root, topdown=False):
dest_dir = os.path.join(
dest_root,
os.path.relpath(path, source_root)
)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)

    for filename in files:
        destFile = os.path.join(dest_dir, filename)
        if os.path.isfile(destFile):
            continue
        os.rename(os.path.join(path, filename), destFile)
        
    for dirname in dirs:
        # remove the source dir since all its contents were moved or are already existent
        shutil.rmtree(os.path.join(path, dirname))
os.rmdir(source_root)
<br>
Sources:
https://stackoverflow.com/a/22588775/2287841
https://unix.stackexchange.com/questions/127712/merging-folders-with-mv/510724#510724
2 upvotes $0.00

Replies (0)

Conversation

No replies yet

Replies will appear here as people join the conversation.

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: