How to merge multiple folders into one folder using Python ?
Last Updated :
28 Apr, 2021
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 path
current_folder = os.getcwd()
# list of folders to be merged
list_dir = ['Folder 1', 'Folder 2', 'Folder 3']
# enumerate on list_dir to get the
# content of all the folders ans store it in a dictionary
content_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 exists
def 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 merged
merge_folder = "merge_folder"
# merge_folder path - current_folder
# + merge_folder
merge_folder_path = os.path.join(current_folder, merge_folder)
# create merge_folder if not exists
make_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 folders
for 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 shutil
import os
# Function to create new folder if not exists
def 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 path
current_folder = os.getcwd()
# list of folders to be merged
list_dir = ['Folder 1', 'Folder 2', 'Folder 3']
# enumerate on list_dir to get the
# content of all the folders ans store
# it in a dictionary
content_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 merged
merge_folder = "merge_folder"
# merge_folder path - current_folder
# + merge_folder
merge_folder_path = os.path.join(current_folder, merge_folder)
# create merge_folder if not exists
make_new_folder(merge_folder, current_folder)
# loop through the list of folders
for 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.py
Folder 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.py
Program in Working :
Similar Reads
How to Merge Multiple Excel Files into one Folder? Power Query is a data manipulation tool frequently used for business intelligence and data analysis. Both Microsoft Excel and Microsoft Power BI support Power Query. A single source of truth and well-organized, error-free data are requirements for high-quality analysis. While many analysts spend man
4 min read
How to Merge Multiple Excel Files into a Single Files with Python We often deal with multiple Excel files that need to be combined into a single consolidated file. Manually merging them or using Excel macros can be slow and prone to errors. To make this process faster and more reliable, we will use Pythonâs pandas module to automate merging multiple Excel files in
3 min read
How to iterate over files in directory using Python? Iterating over files in a directory using Python involves accessing and processing files within a specified folder. Python provides multiple methods to achieve this, depending on efficiency and ease of use. These methods allow listing files, filtering specific types and handling subdirectories.Using
3 min read
How to Merge all excel files in a folder using Python? In this article, we will see how to combine all Excel files present in a folder into a single file. Module used: The python libraries used are: Pandas: Pandas is a python library developed for a python programming language for manipulating data and analyzing the data. It is widely used in Data Scien
3 min read
How to run multiple Python file in a folder one after another? In this article, we will discuss how to run multiple python files in a folder one after another. There can be many ways to this task, here, we will discuss a few of them. For doing this program, we have to create some python files in one folder and give some names to that folder. The content inside
3 min read
Make multiple directories based on a List using Python In this article, we are going to learn how to make directories based on a list using Python. Python has a Python module named os which forms the core part of the Python ecosystem. The os module helps us to work with operating system folders and other related functionalities. Although we can make fol
3 min read