Created
June 28, 2025 18:36
-
-
Save me-suzy/dd16c7c57c92d07f88f494b5f8c60d89 to your computer and use it in GitHub Desktop.
Sterge toate subfolderele care nu contin fisiere pdf .py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
def delete_folders_without_pdfs(folder_path): | |
""" | |
Șterge automat toate subfolderele care nu conțin PDF-uri | |
""" | |
folder_path = folder_path.replace("\\", "/") | |
print(f"=== CURĂȚARE AUTOMATĂ SUBFOLDERE ===") | |
print(f"Director: {folder_path}") | |
if not os.path.exists(folder_path): | |
print(f"EROARE: Directorul {folder_path} nu există!") | |
return | |
# Statistici | |
total_subfolders = 0 | |
folders_with_pdfs = 0 | |
folders_deleted = 0 | |
errors = 0 | |
print(f"\n=== SCANARE ȘI ȘTERGERE ===") | |
# Colectează toate folderele care trebuie șterse | |
folders_to_delete = [] | |
for current_dir, subdirs, files in os.walk(folder_path): | |
# Skip directorul principal | |
if current_dir == folder_path: | |
continue | |
total_subfolders += 1 | |
# Verifică PDF-uri în folder | |
pdf_files = [f for f in files if f.lower().endswith('.pdf')] | |
if pdf_files: | |
folders_with_pdfs += 1 | |
print(f"✓ PĂSTREAZĂ: {current_dir} ({len(pdf_files)} PDF-uri)") | |
else: | |
folders_to_delete.append(current_dir) | |
other_files = [f for f in files if not f.lower().endswith('.pdf')] | |
print(f"✗ MARCHEAZĂ PENTRU ȘTERGERE: {current_dir} ({len(other_files)} fișiere non-PDF)") | |
print(f"\n=== ȘTERGERE ÎN CURS ===") | |
# Sortează în ordine inversă pentru a șterge mai întâi subfolderele | |
folders_to_delete_sorted = sorted(folders_to_delete, key=len, reverse=True) | |
for i, folder in enumerate(folders_to_delete_sorted, 1): | |
try: | |
if os.path.exists(folder): | |
shutil.rmtree(folder) | |
print(f"{i:3d}. ✓ ȘTERS: {folder}") | |
folders_deleted += 1 | |
else: | |
print(f"{i:3d}. ⚠ Nu mai există: {folder}") | |
except Exception as e: | |
print(f"{i:3d}. ✗ EROARE: {folder} - {str(e)}") | |
errors += 1 | |
# Raport final | |
print(f"\n=== RAPORT FINAL ===") | |
print(f"Total subfoldere scanate: {total_subfolders}") | |
print(f"Subfoldere cu PDF-uri (păstrate): {folders_with_pdfs}") | |
print(f"Subfoldere șterse cu succes: {folders_deleted}") | |
print(f"Erori: {errors}") | |
if folders_deleted > 0: | |
print(f"\n🎯 CURĂȚARE COMPLETĂ! {folders_deleted} subfoldere fără PDF-uri eliminate.") | |
else: | |
print(f"\n✅ Nu au fost găsite subfoldere fără PDF-uri.") | |
# Versiune simplă pentru apel rapid | |
def cleanup_no_pdfs(folder_path): | |
"""Apel simplu pentru curățare""" | |
delete_folders_without_pdfs(folder_path) | |
# Versiune cu raportare detaliată | |
def detailed_cleanup(folder_path): | |
""" | |
Versiune cu raportare mai detaliată a conținutului | |
""" | |
folder_path = folder_path.replace("\\", "/") | |
print(f"=== CURĂȚARE DETALIATĂ ===") | |
print(f"Director: {folder_path}") | |
folders_deleted = 0 | |
folders_kept = 0 | |
for current_dir, subdirs, files in os.walk(folder_path): | |
if current_dir == folder_path: | |
continue | |
pdf_files = [f for f in files if f.lower().endswith('.pdf')] | |
other_files = [f for f in files if not f.lower().endswith('.pdf')] | |
if pdf_files: | |
folders_kept += 1 | |
print(f"\n✓ PĂSTREAZĂ: {current_dir}") | |
print(f" PDF-uri: {len(pdf_files)}") | |
for pdf in pdf_files[:3]: # Primele 3 | |
print(f" - {pdf}") | |
if len(pdf_files) > 3: | |
print(f" ... și încă {len(pdf_files) - 3}") | |
else: | |
print(f"\n✗ ȘTERGE: {current_dir}") | |
if other_files: | |
print(f" Conține: {len(other_files)} fișiere non-PDF") | |
for f in other_files[:3]: # Primele 3 | |
print(f" - {f}") | |
if len(other_files) > 3: | |
print(f" ... și încă {len(other_files) - 3}") | |
else: | |
print(f" Folder gol") | |
# Șterge imediat | |
try: | |
shutil.rmtree(current_dir) | |
print(f" ✓ ȘTERS") | |
folders_deleted += 1 | |
except Exception as e: | |
print(f" ✗ EROARE: {str(e)}") | |
print(f"\n=== REZULTAT FINAL ===") | |
print(f"Subfoldere păstrate: {folders_kept}") | |
print(f"Subfoldere șterse: {folders_deleted}") | |
# Rulare automată | |
if __name__ == "__main__": | |
folder_path = "e:/De pus pe FTP 2/1" | |
# Apel simplu - doar șterge și raportează | |
cleanup_no_pdfs(folder_path) | |
# SAU pentru raportare detaliată: | |
# detailed_cleanup(folder_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment