Files
novafarma/flatten_refs.py

53 lines
1.5 KiB
Python

import os
import shutil
TARGET = "assets/slike/glavna_referenca"
def flatten():
print(f"🚀 FLATTENING {TARGET} (Removing subfolders)...")
if not os.path.exists(TARGET):
print("❌ Target folder not found.")
return
moved_count = 0
# Walk bottom-up to handle sub-sub folders if any
for root, dirs, files in os.walk(TARGET, topdown=False):
if root == TARGET:
continue
for file in files:
src = os.path.join(root, file)
dst = os.path.join(TARGET, file)
# Handle collisions
if os.path.exists(dst):
base, ext = os.path.splitext(file)
counter = 1
while os.path.exists(os.path.join(TARGET, f"{base}_{counter}{ext}")):
counter += 1
dst = os.path.join(TARGET, f"{base}_{counter}{ext}")
try:
shutil.move(src, dst)
moved_count += 1
except Exception as e:
print(f"Error moving {file}: {e}")
# Remove empty dir
try:
os.rmdir(root)
print(f"🗑️ Removed empty folder: {os.path.relpath(root, TARGET)}")
except OSError:
# Folder might not be empty if hidden files exist
pass
print("="*40)
print(f"✅ FLATTEN COMPLETE. Moved {moved_count} images to root.")
print("="*40)
if __name__ == "__main__":
flatten()