48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
|
|
import os
|
||
|
|
import shutil
|
||
|
|
|
||
|
|
def merge_directories(src1, src2, dest):
|
||
|
|
if not os.path.exists(dest):
|
||
|
|
os.makedirs(dest)
|
||
|
|
|
||
|
|
def copy_or_merge(src, dest):
|
||
|
|
for patient in os.listdir(src):
|
||
|
|
patient_src_path = os.path.join(src, patient)
|
||
|
|
patient_dest_path = os.path.join(dest, patient)
|
||
|
|
|
||
|
|
if os.path.isdir(patient_src_path):
|
||
|
|
if os.path.exists(patient_dest_path):
|
||
|
|
# If destination folder exists, merge the contents
|
||
|
|
for item in os.listdir(patient_src_path):
|
||
|
|
s = os.path.join(patient_src_path, item)
|
||
|
|
d = os.path.join(patient_dest_path, item)
|
||
|
|
if os.path.isdir(s):
|
||
|
|
shutil.copytree(s, d, dirs_exist_ok=True)
|
||
|
|
else:
|
||
|
|
shutil.copy2(s, d)
|
||
|
|
else:
|
||
|
|
# If destination folder does not exist, copy the whole folder
|
||
|
|
shutil.copytree(patient_src_path, patient_dest_path)
|
||
|
|
|
||
|
|
# Copy or merge from both source directories to destination directory
|
||
|
|
copy_or_merge(src1, dest)
|
||
|
|
copy_or_merge(src2, dest)
|
||
|
|
|
||
|
|
print("Directories merged successfully.")
|
||
|
|
|
||
|
|
# Define source and destination paths
|
||
|
|
src1 = '/mnt/1220/Public/dataset2/G4'
|
||
|
|
src2 = '/mnt/1220/Public/dataset2/M6'
|
||
|
|
dest = "/home/onlylian/dataset2_G4_M6"
|
||
|
|
|
||
|
|
# Call the function to merge the directories
|
||
|
|
merge_directories(src1, src2, dest)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|