简体   繁体   中英

Merge two folders in python

I need to merge two folders,

The folders are named 12345 and 12345_

How would I merge the two?

I have tried but I end up with '12345.'

for file in files:
    subFolder = os.path.join(destpath, file[:6])
    if not os.path.isdir(subFolder):
        os.makedirs(subFolder)
    shutil.copy(os.path.join(root, file), subFolder)

I need to merge two folders,

The folders are named 12345 and 12345_

How would I merge the two?

I have tried but I end up with '12345.'

for file in files:
    subFolder = os.path.join(destpath, file[:6])
    if not os.path.isdir(subFolder):
        os.makedirs(subFolder)
    shutil.copy(os.path.join(root, file), subFolder)

I need to merge two folders,

The folders are named 12345 and 12345_

How would I merge the two?

I have tried but I end up with '12345.'

for file in files:
    subFolder = os.path.join(destpath, file[:6])
    if not os.path.isdir(subFolder):
        os.makedirs(subFolder)
    shutil.copy(os.path.join(root, file), subFolder)

I wrote a simple recursive function in pure python that does not call any shell command. In this exemple, all the contents (files and directories) of folder1 will be merge in folder2:

import os
import shutil

def merge(scr_path, dir_path):
  files = next(os.walk(scr_path))[2]
  folders = next(os.walk(scr_path))[1]
  for file in files: # Copy the files
    scr_file = scr_path + "/" + file
    dir_file = dir_path + "/" + file
    if os.path.exists(dir_file): # Delete the old files if already exist
      os.remove(dir_file)
    shutil.copy(scr_file, dir_file)
  for folder in folders: # Merge again with the subdirectories
    scr_folder = scr_path + "/" + folder
    dir_folder = dir_path + "/" + folder
    if not os.path.exists(dir_folder): # Create the subdirectories if dont already exist
      os.mkdir(dir_folder)
    merge(scr_folder, dir_folder)

path1 = "path/to/folder1"
path2 = "path/to/folder2"

merge(path1, path2)

If replacing the files in the destination directory, if they already exist, is acceptable, then since Python 3.8, this can be achieved easily with shutil.copytree ;

import shutil
shutil.copytree("src_root", "dst", dirs_exist_ok=True)

From the documentation here :

Recursively copy an entire directory tree rooted at src to a directory named dst and return the destination directory. All intermediate directories needed to contain dst will also be created by default.

If dirs_exist_ok is false (the default) and dst already exists, a FileExistsError is raised. If dirs_exist_ok is true, the copying operation will continue if it encounters existing directories, and files within the dst tree will be overwritten by corresponding files from the src tree.

New in version 3.8: The dirs_exist_ok parameter.

How about this option to write to Excel sheets?

import xlsxwriter


# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('C:/Users/your_path_here/Excel_File.xlsx')
worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})

# Write some simple text.
worksheet.write('A1', 'Hello')

# Text with formatting.
worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)


workbook.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM