修改字幕文件名字脚本

# -*- coding: utf-8 -*-
import os
import re


def getfilelist(path="", filetype=".txt"):
    filelist = []
    try:
        filenames = os.listdir(path)
        if (len(filenames) > 0):
            for fn in filenames:
                if fn.endswith(filetype):
                    fullfilename = os.path.join(path, fn)
                    filelist.append(fullfilename)
        return filelist
    except FileNotFoundError:
        print("请输入目录")


path = input("please enter your dir: ")
mkvfilelist = getfilelist(path, '.mkv')
assfilelist = getfilelist(path, '.ass')

if mkvfilelist and assfilelist:
    for i in mkvfilelist:
        for j in assfilelist:
            name = re.search(r'(S\d{2}E\d{2})', j).group(1)
            if name in i:
                newname = i.split(".", 1)[0] + ".ass"
                os.rename(j, newname)