Files
rename_anime_episodes/rename.py
2024-05-12 18:26:49 +07:00

29 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from sys import exit
from os import listdir, rename
from os.path import splitext, realpath
from re import search
number_pattern = r"\[\d{1,}\]" # Паттерн для поиска номера серии
extension_versions = [".mkv", ".avi"] # Список поддерживаемых расширений
def rename_file(full_path, file_name):
found_number = search(number_pattern, file_name[0]) # Ищем номер серии, если он есть то продолжаем
if found_number is not None:
found_number = found_number.group()[1:-1] # Убираем квадратные скобки
current_path = realpath(full_path) # Получаем путь до файла
new_path = "{}{}{}".format(current_path.replace(full_path, ""), found_number, file_name[1]) # Новый путь до файла
rename(current_path, new_path) # Переименовываем файл
print("\"{}\" successfully renamed to \"{}\".".format(full_path, found_number + file_name[1]))
if __name__ == '__main__':
for file_name in listdir():
splitted_name = splitext(file_name) # Разделяем имя файла и его расширение, итог: кортеж (0 - имя файла, 1 - расширение)
if splitted_name[1] in extension_versions: # Проверяем совпадения расширения со списком, если есть совпадения то переименовываем файл
rename_file(file_name, splitted_name) # Вызываем функцию переименования файла
exit()