-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_combine.py
49 lines (38 loc) · 1.74 KB
/
data_combine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import shutil
def combine_folders(folder1, folder2, destination_folder):
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
for root, dirs, files in os.walk(folder1):
relative_path = os.path.relpath(root, folder1)
dest_path = os.path.join(destination_folder, relative_path)
if not os.path.exists(dest_path):
os.makedirs(dest_path)
for file in files:
src_file = os.path.join(root, file)
dest_file = os.path.join(dest_path, file)
if not os.path.exists(dest_file):
shutil.copy2(src_file, dest_file)
for root, dirs, files in os.walk(folder2):
relative_path = os.path.relpath(root, folder2)
dest_path = os.path.join(destination_folder, relative_path)
if not os.path.exists(dest_path):
os.makedirs(dest_path)
for file in files:
src_file = os.path.join(root, file)
dest_file = os.path.join(dest_path, file)
if not os.path.exists(dest_file):
shutil.copy2(src_file, dest_file)
else:
base, extension = os.path.splitext(file)
counter = 1
new_dest_file = os.path.join(dest_path, f"{base}_{counter}{extension}")
while os.path.exists(new_dest_file):
counter += 1
new_dest_file = os.path.join(dest_path, f"{base}_{counter}{extension}")
shutil.copy2(src_file, new_dest_file)
folder1 = 'Folder 1'
folder2 = 'Folder 2'
destination_folder = 'Output Folder'
#Note both the folder1 and 2 must be in the same format.
combine_folders(folder1, folder2, destination_folder)