Advertisement
nicuf

muta fisierele pdf in foldere cu acelasi nume

Jan 13th, 2024 (edited)
1,770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import os
  2. import shutil
  3.  
  4. folder_path = r"j:\DE SCANAT\GATA - doar de scanat in txt\NOI BIBLIOTECA"
  5.  
  6. # Parcurge fiecare fișier din director
  7. for file_name in os.listdir(folder_path):
  8.     if file_name.endswith(".pdf"):
  9.         # Verifică dacă fișierul corespunde formatului dorit
  10.         if " - " in file_name:
  11.             # Extrage numele de fișier până la " - "
  12.             folder_name = file_name.split(" - ")[0].strip()
  13.  
  14.             # Calea completă pentru fișier și folder
  15.             file_path = os.path.join(folder_path, file_name)
  16.             folder_path_new = os.path.join(folder_path, folder_name)
  17.  
  18.             # Crează folderul dacă nu există deja
  19.             if not os.path.exists(folder_path_new):
  20.                 os.mkdir(folder_path_new)
  21.  
  22.             # Mută fișierul în noul folder
  23.             shutil.move(file_path, os.path.join(folder_path_new, file_name))
  24.         else:
  25.             # Afiseaza numele fișierului care nu a putut fi procesat
  26.             print(f"Nu s-a putut crea folder pentru: {file_name}")
  27.  
  28. print("Procesul a fost finalizat.")
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement