Created
January 1, 2025 07:46
-
-
Save me-suzy/f07ddcf6fb5aa236b4e65f1b9a20f6b1 to your computer and use it in GitHub Desktop.
muta fisierele pdf in foldere cu acelasi nume 2025 BUN.py
This file contains 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 | |
import re | |
def clean_filename(filename): | |
# Eliminăm parantezele rotunde și conținutul lor | |
cleaned = re.sub(r'\s*\([^)]*\)', '', filename) | |
# Înlocuim caracterele non-alfanumerice (exceptând spațiile și cratimele) cu underscore | |
cleaned = re.sub(r'[^a-zA-Z0-9 \-_]', '_', cleaned) | |
# Eliminăm spațiile multiple și spațiile de la început și sfârșit | |
cleaned = ' '.join(cleaned.split()) | |
return cleaned | |
def truncate_title(title, max_words=7): | |
words = title.split() | |
if len(words) <= max_words: | |
return title | |
return ' '.join(words[:max_words]) + '...' | |
def safe_move(src, dst): | |
try: | |
max_path = 260 # Limita Windows pentru calea completă | |
if len(dst) > max_path: | |
base_dst = os.path.dirname(dst) | |
file_name = os.path.basename(src) | |
available_length = max_path - len(base_dst) - 1 | |
truncated_name = file_name[:available_length] | |
dst = os.path.join(base_dst, truncated_name) | |
shutil.move(src, dst) | |
print(f"Fișierul mutat cu succes: {src} -> {dst}") | |
except Exception as e: | |
print(f"Eroare la mutarea fișierului {src}: {str(e)}") | |
folder_path = r"e:\De pus pe FTP 2" | |
for file_name in os.listdir(folder_path): | |
if file_name.endswith(".pdf"): | |
# Standardizăm separatorul | |
file_name_standardized = file_name.replace(" – ", " - ").replace(" - ", " - ") | |
if " - " in file_name_standardized: | |
parts = file_name_standardized.split(" - ", 1) | |
if len(parts) == 2: | |
author_name = parts[0].strip() # Păstrăm numele autorului exact așa cum este | |
# Curățăm titlul de paranteze și conținutul lor înainte de a-l trunchia | |
book_title_raw = parts[1].strip().rsplit('.', 1)[0] | |
book_title = clean_filename(book_title_raw) | |
file_path = os.path.join(folder_path, file_name) | |
author_folder_path = os.path.join(folder_path, author_name) | |
book_folder_path = os.path.join(author_folder_path, book_title) | |
try: | |
os.makedirs(book_folder_path, exist_ok=True) | |
print(f"Director creat: {book_folder_path}") | |
except OSError as e: | |
print(f"Eroare la crearea directorului: {str(e)}") | |
continue | |
safe_move(file_path, os.path.join(book_folder_path, file_name)) | |
else: | |
print(f"Format neașteptat pentru fișierul: {file_name}") | |
else: | |
print(f"Separatorul ' - ' nu este prezent în numele fișierului: {file_name}") | |
print("Procesul a fost finalizat.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment