Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from googletrans import Translator
- import os
- import re
- def translate_and_format_name(english_name):
- translator = Translator()
- translated = translator.translate(english_name, src='en', dest='bg')
- translated_text = translated.text.lower().replace(" ", "_")
- translated_text = re.sub(r'[-\s]+', '_', translated_text)
- translated_text = re.sub(r'_+', '_', translated_text)
- return translated_text
- def rename_directory(old_path, new_path):
- try:
- os.rename(old_path, new_path)
- print(f'Името на директорията "{old_path}" е успешно променено на "{new_path}"')
- except OSError as e:
- print(f'Грешка при промяната на името на директорията "{old_path}": {e}')
- main_directory = input(r'C:\Users\Alka\Downloads')
- confirmation = input(f'Сигурни ли сте, че искате да преименувате директориите в "{main_directory}"? (да/не): ')
- if confirmation.lower() != 'да':
- print('Операцията беше отказана.')
- else:
- for dirpath, dirnames, filenames in os.walk(main_directory):
- for dirname in dirnames:
- old_path = os.path.join(dirpath, dirname)
- new_name_bg = translate_and_format_name(dirname)
- new_path = os.path.join(dirpath, new_name_bg)
- if new_name_bg == dirname:
- print(f'Прескачане на директорията "{dirname}", тъй като е вече преведено и форматирано.')
- continue
- rename_directory(old_path, new_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement