How to merge multiple folders into one folder using Python ?
In this article, we will discuss how to move multiple folders into one folder. This can be done using Python’s OS and Shutil module.
Approach:
- Get the current directory and the list of the folders you want to merge.
- Loop through the list of folders and store their content in a list. Here, we have stored them in the dictionary so that we can have the name of the folder as a key and its content as a value list.
- Specify the folder in which you want to merge all the other folders. If the folder exists then we are good to go but if the folder does not exist then create a new folder.
- Loop through the dictionary and move all the content of all the listed folders inside the merge folder.
Let’s implement this approach step by step:
Step 1: Below code does the following:
- Get the current directory.
- List all the folders that you want to merge.
- Stores content of all the listed folders in the dictionary with folder name as key and its content as a value list.
Python3
# current folder pathcurrent_folder = os.getcwd() # list of folders to be mergedlist_dir = ['Folder 1', 'Folder 2', 'Folder 3'] # enumerate on list_dir to get the # content of all the folders ans store it in a dictionarycontent_list = {}for index, val in enumerate(list_dir): path = os.path.join(current_folder, val) content_list[ list_dir[index] ] = os.listdir(path) |
Step 2: Creates the merge folder if it does not already exist.
Python3
# Function to create new folder if not existsdef make_new_folder(folder_name, parent_folder_path): # Path path = os.path.join(parent_folder_path, folder_name) # Create the folder # 'new_folder' in # parent_folder try: # mode of the folder mode = 0o777 # Create folder os.mkdir(path, mode) except OSError as error: print(error) # folder in which all the content # will be mergedmerge_folder = "merge_folder" # merge_folder path - current_folder # + merge_foldermerge_folder_path = os.path.join(current_folder, merge_folder) # create merge_folder if not existsmake_new_folder(merge_folder, current_folder) |
Step 3: Below code does the following:
- Loop through the dictionary with all the folders.
- Now loop through the content of each folder and one by one move them to the merge folder.
Python3
# loop through the list of foldersfor sub_dir in content_list: # loop through the contents of the # list of folders for contents in content_list[sub_dir]: # make the path of the content to move path_to_content = sub_dir + "/" + contents # make the path with the current folder dir_to_move = os.path.join(current_folder, path_to_content ) # move the file shutil.move(dir_to_move, merge_folder_path) |
Complete Code:
Python3
import shutilimport os # Function to create new folder if not existsdef make_new_folder(folder_name, parent_folder): # Path path = os.path.join(parent_folder, folder_name) # Create the folder # 'new_folder' in # parent_folder try: # mode of the folder mode = 0o777 # Create folder os.mkdir(path, mode) except OSError as error: print(error) # current folder pathcurrent_folder = os.getcwd() # list of folders to be mergedlist_dir = ['Folder 1', 'Folder 2', 'Folder 3'] # enumerate on list_dir to get the # content of all the folders ans store # it in a dictionarycontent_list = {}for index, val in enumerate(list_dir): path = os.path.join(current_folder, val) content_list[ list_dir[index] ] = os.listdir(path) # folder in which all the content will# be mergedmerge_folder = "merge_folder" # merge_folder path - current_folder # + merge_foldermerge_folder_path = os.path.join(current_folder, merge_folder) # create merge_folder if not existsmake_new_folder(merge_folder, current_folder) # loop through the list of foldersfor sub_dir in content_list: # loop through the contents of the # list of folders for contents in content_list[sub_dir]: # make the path of the content to move path_to_content = sub_dir + "/" + contents # make the path with the current folder dir_to_move = os.path.join(current_folder, path_to_content ) # move the file shutil.move(dir_to_move, merge_folder_path) |
Folder structure before running the above program.
Folder 1
File 1
File 2
Folder 2
File 3
File 4
Folder 3
File 5
File 6
Folder 4
File 7
File 8
merge_folder (Empty)
move_script.pyFolder structure after running the above program.
Folder 1 (Empty)
Folder 2 (Empty)
Folder 3 (Empty)
Folder 4 (Untouched)
File 7
File 8
merge_folder
File 1
File 2
File 3
File 4
File 5
File 6
move_script.pyProgram in Working :







Please Login to comment...