eclipticwonder's picture
Upload folder using huggingface_hub (part 8)
69aa3f7 verified
Raw
History Blame Contribute Delete
1.07 kB
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.")