File size: 1,263 Bytes
7bdf62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import shutil

# Directories configuration
dirs = {
    'image': 'jpg',
    'image-parse-v3': 'png',
    'cloth': 'jpg',
    'cloth-mask': 'jpg'
}
spare_dir = 'spare'

# Ensure the spare directory exists
os.makedirs(spare_dir, exist_ok=True)

# Function to get base names of files in a directory
def get_base_names(dir_path, ext):
    return {os.path.splitext(f)[0] for f in os.listdir(dir_path) if f.endswith('.' + ext)}

# Collect base names from all directories
base_names = {}
for dir_name, ext in dirs.items():
    base_names[dir_name] = get_base_names(dir_name, ext)

# Identify files without a match in all directories
spare_files = set()
for dir_name, names in base_names.items():
    # Find files that do not have a match in every other directory
    other_dirs = set(dirs.keys()) - {dir_name}
    for base_name in names:
        if not all(base_name in base_names[other_dir] for other_dir in other_dirs):
            spare_files.add((dir_name, base_name + '.' + dirs[dir_name]))

# Move spare files to the spare directory
for dir_name, file_name in spare_files:
    src = os.path.join(dir_name, file_name)
    dst = os.path.join(spare_dir, file_name)
    shutil.move(src, dst)
    print(f"Moved {src} to {dst}")

print("Operation completed.")