How to merge two folders using python ?

Created November 24, 2022 | Edited November 24, 2022 | Viewed 89 | by Benjamin Edit


Example of how to merge two folders using python

Introduction

I used google takeout to download a project that has been saved on google drive. However since the project is big, the project has been divided in two folders during the download:

Takeout_01

and

Takeout_02

folders.

Folders paths in my local computer:

takeout_01_path = '/Users/JDoe/Downloads/Takeout_01'
takeout_02_path = '/Users/JDoe/Downloads/Takeout_02'

The goal was then to create a simple python code to merge Takeout_02 into Takeout_01.

Iterate over all files and directories

First step iterate over all files in Takeout_02:

# Loop over all files from takeout_02_path

for path, subdirs, files in os.walk(takeout_02_path):

    print(path)
    print(files)
    print()

gives for example:

/Users/JDoe/Downloads/Takeout_01/Drive/Deep_Work/ideas4eo_v2/archive/sent_to_digital_ocean/first version/ideas4eo/myproject
['__init__.py', 'settings.py', 'urls.py', 'wsgi.py']

/Users/JDoe/Downloads/Takeout_01/Drive/Deep_Work/ideas4eo_v2/archive/sent_to_digital_ocean/first version/ideas4eo/myproject/__pycache__
['settings.cpython-35.pyc', 'settings.cpython-36.pyc', 'wsgi.cpython-35.pyc', 'wsgi.cpython-36.pyc', '__init__.cpython-36.pyc', 'urls.cpython-36.pyc', '__init__.cpython-35.pyc', 'urls.cpython-35.pyc']

Check if directory exists if not create it

    path_tmp = path.replace(takeout_02_path,'')
    path_tmp_folder_list = path_tmp.split('/')

    del path_tmp_folder_list[0]

    if len( path_tmp_folder_list ) > 0:

        current_path = takeout_01_path

        for folder_name in path_tmp_folder_list:

            current_path += '/' + folder_name

            if not os.path.exists(current_path):

                ## Special Characters

                current_path = current_path.replace(' ', '\ ' )

                cmd = 'mkdir {}'.format(current_path)

                os.system( cmd )

Move all files from Takeout_02 to Takeout_01

    for name in files:

        src = os.path.join(path, name)
        trg = path.replace('Takeout_02','Takeout_01') + '/.'

        ## Special Characters

        src = src.replace(' ','\ ' )
        src = src.replace('(','\(' )
        src = src.replace(')','\)' )

        trg = trg.replace(' ','\ ' )
        trg = trg.replace('(','\(' )
        trg = trg.replace(')','\)' )

        cmd = "mv {} {}".format(src,trg)

        #print('tcon',cmd)

        os.system( cmd )

Note: Got the error Syntax Error Near Unexpected Token ‘(‘, to fix that it is important to remove Special Characters and Quotingin file and path names.

Full code

import os

takeout_01_path = '/Users/JDoe/Downloads/Takeout_01'
takeout_02_path = '/Users/JDoe/Downloads/Takeout_02'

# Loop over all files from takeout_02_path
for path, subdirs, files in os.walk(takeout_02_path):

    ### check if path exists in takeout_01 if not create it.

    path_tmp = path.replace(takeout_02_path,'')
    path_tmp_folder_list = path_tmp.split('/')

    del path_tmp_folder_list[0]

    if len( path_tmp_folder_list ) > 0:

        current_path = takeout_01_path

        for folder_name in path_tmp_folder_list:

            current_path += '/' + folder_name

            if not os.path.exists(current_path):

                ## Special Characters

                current_path = current_path.replace(' ', '\ ' )

                cmd = 'mkdir {}'.format(current_path)

                os.system( cmd )

    ### Move file to takeout_01

    for name in files:

        src = os.path.join(path, name)
        trg = path.replace('Takeout_02','Takeout_01') + '/.'

        ## Special Characters

        src = src.replace(' ','\ ' )
        src = src.replace('(','\(' )
        src = src.replace(')','\)' )

        trg = trg.replace(' ','\ ' )
        trg = trg.replace('(','\(' )
        trg = trg.replace(')','\)' )

        cmd = "mv {} {}".format(src,trg)

        #print('tcon',cmd)

        os.system( cmd )

        #print()

See also