7

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)
fdermishin
  • 3,519
  • 3
  • 24
  • 45
lithocrat
  • 71
  • 1
  • 1
  • 6

4 Answers4

3

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.

makeVoiceBot
  • 143
  • 7
1

You could use something like this, which copies all the files from folder one to folder two so folder two will have all files from one and two:

#!/usr/bin/env python

import subprocess as sbp
import os

path=raw_input('Please enter a path\n')
fol = os.listdir(path)
p2 = raw_input('Please enter a path\n')

for i in fol:
    p1 = os.path.join(path,i)
    p3 = 'cp -r ' + p1 +' ' + p2+'/.'
    sbp.Popen(p3,shell=True)
Omi Harjani
  • 737
  • 1
  • 8
  • 20
1

From python, you could also call rsync, which is made for this.

import subprocess

## define your paths
path1 = '/path/to/12345/'
path2 = '/path/to/12345_/'

## where to place the merged data
merged_path = '/path/to/merged/'

## write an rsync commands to merge the directories
rsync_cmd = 'rsync' + ' -avzh ' + path1 + ' ' + path2 + ' ' + merged_path

## run the rsync command
subprocess.run(rsync_cmd, shell=True)

Alex Witsil
  • 855
  • 8
  • 22
  • What does `-avzh` mean as an `rsync` argument? – étale-cohomology May 05 '22 at 23:02
  • **v** - verbose **a** - archive mode, which allows copying files recursively and also preserves symbolic links, file permissions, user & group ownerships, and timestamps **z** - compress file data **h** - human-readable – Alex Witsil May 18 '22 at 16:17
0

I wrote a simple recursive function in pure python that merge a folder and its content into an other:

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)

Here the folder1 is merged into folder2.

For every folder1 elements, it check if the element already exist in the folder2, if so the element is replaced, if not the element is created.

In the end, the merged folder contains its original elements plus the elements of the other folder.

Marin Nagy
  • 297
  • 1
  • 10
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 20 '22 at 15:58