📌  相关文章
📜  如何使用Python将多个文件夹合并到一个文件夹中?

📅  最后修改于: 2022-05-13 01:55:27.401000             🧑  作者: Mango

如何使用Python将多个文件夹合并到一个文件夹中?

在本文中,我们将讨论如何将多个文件夹移动到一个文件夹中。这可以使用 Python 的OSShuutil模块来完成。

方法:

  1. 获取当前目录和要合并的文件夹列表。
  2. 遍历文件夹列表并将其内容存储在列表中。在这里,我们将它们存储在字典中,以便我们可以将文件夹的名称作为键,并将其内容作为值列表。
  3. 指定要合并所有其他文件夹的文件夹。如果文件夹存在,那么我们很高兴,但如果文件夹不存在,则创建一个新文件夹。
  4. 遍历字典并将所有列出的文件夹的所有内容移动到合并文件夹中。

让我们一步一步地实现这个方法:

第 1 步:下面的代码执行以下操作:

  • 获取当前目录。
  • 列出要合并的所有文件夹。
  • 存储字典中所有列出的文件夹的内容,以文件夹名称为键,其内容为值列表。
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)


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)


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)


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)


第 2 步:如果合并文件夹尚不存在,则创建该文件夹。



蟒蛇3

# 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)

第 3 步:下面的代码执行以下操作:

  • 用所有文件夹循环字典。
  • 现在循环遍历每个文件夹的内容,并将它们一一移动到合并文件夹。

蟒蛇3

# 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)

完整代码:

蟒蛇3

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 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 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

工作计划: