File size: 1,071 Bytes
69aa3f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, time
import shutil
from tqdm import tqdm

folders_dir = r"C:\Users\Abzu\Desktop\diffusiondb\extracted data"

target_dir = r"C:\Users\Abzu\Desktop\diffusiondb\all-images"

os.makedirs(target_dir, exist_ok=True)

folders_dir = os.path.abspath(folders_dir)
target_dir = os.path.abspath(target_dir)

for root, dirs, files in os.walk(folders_dir, topdown=False):

    if os.path.commonpath([root, target_dir]) == target_dir:
        continue

    for file in tqdm(files, desc = root):
        src = os.path.join(root, file)
        dst = os.path.join(target_dir, file)

        if os.path.exists(dst):
            name, ext = os.path.splitext(file)
            counter = 1
            while os.path.exists(dst):
                dst = os.path.join(target_dir, f"{name}_{counter}{ext}")
                counter += 1

        shutil.move(src, dst)

    if root != folders_dir:
        try:
            os.rmdir(root)
        except OSError:
            pass 

print("✅ All files moved to 'all-images' and empty folders deleted.")