如何比较python中的文件名?

时间:2019-02-17 07:43:05

标签: python file operating-system

我已经厌倦了用字幕来重命名每一集(我有100 +)

如果两个文件名称中的情节编号相同,如何比较这两个文件,之后我希望将其替换为情节名称的副标题

我尝试自己做,成功地更改了名称(简单),但是比较部分是我所缺少的。

谢谢。

已编辑:

文件名将是这样;

(片段):[PuyaSubs!]香蕉鱼-12 [1080p] .mkv

(字幕):香蕉鱼-12.ass

到目前为止我想到的是什么

import os
old_file = os.path.join(r"D:\Downloads\Media\Anime\[PuyaSubs!] Banana Fish [1080p][Batch]\Banana Fish - 12.ass", "Banana Fish - 12.ass")
new_file = os.path.join(r"D:\Downloads\Media\Anime\[PuyaSubs!] Banana Fish [1080p][Batch]\Banana Fish - 12.ass", "[PuyaSubs!] Banana Fish - 12 [1080p][2ECC9207].ass")
os.rename(old_file, new_file)

这只会更改名称,但不会自动更改,我将必须手动处理每个字幕文件。

2 个答案:

答案 0 :(得分:2)

我看待这个问题的方式是这样的:

我如何实现此目标:

  1. 首先使用os.listdir
  2. 获取目录中所有文件的列表
  3. 接下来,我们遍历每个文件,如果文件包含后缀.ass(这表示它是字幕文件)
  4. 然后,我们遍历整个文件目录,查看是否有与字幕文件匹配的电影。我们在try语句中执行此操作,这使我们能够突破for循环,从而避免出现任何错误。
  5. 最后,我们只需要重命名字幕文件名,为此,我们使用os.rename
#!/usr/bin/env python3

import os, re

def fixMySubtitles(mediaFolderPath):
    # Lists all the files in the folder
    files = os.listdir(mediaFolderPath)

    # Loops through every single file in the directory
    for file in files:
        # In the beginning we set both names to "" since we don't know what they are yet.
        movie = ""
        subtitle = ""

        # If searches the current file name to see if it ends in .ass
        if re.search(r'\.ass', file):
            # This removes the .ass part from the subtitle name
            subtitle = re.search(r'(.*).ass', file).group(1)

            # This chunk matches the subtitle file to the movie file
            movie = [movie for movie in files if subtitle in movie]
            movie = [movie for movie in movie if '.mkv' in movie]
            movie = "".join(movie)

            # Now we try to get the movie without the .mkv part
            try:
                movie = re.search(r'(.*).mkv', movie).group(1)
            except:
                print("Movie not found error for subtitle '%s'" % (file))
                break

            # Lastly we can set the path of the new subtitle file
            newPath = os.path.join(mediaFolderPath, "%s.ass" % (movie))

            # And if the movie variable has a value then we can rename
            if movie:
                os.rename(os.path.join(mediaFolderPath, file), newPath)

    print("I have fixed your subtitles!")

如何运行

我现在实际上是在Mac上,但是在Windows上仍然可以使用。

  1. 运行此程序时,您将需要一个包含代码的python文件。
  2. 一旦您准备好了这段代码,您将需要实际调用该方法,因为现在我们已经使用def创建了函数(函数/方法可以互换使用),但是实际上我们并没有运行在任何地方都可以使用
  3. 在代码底部添加一行,内容为
    fixMySubtitles("[DIRECTORY OF FOLDER WITH MEDIA]")并将[DIRECTORY OF FOLDER...]替换为文件夹的路径
  4. 最后,您应该可以使用 python.exe [PATH TO PYTHON FILE]

作为参考,这是我在文件中所包含的确切代码:

#!/usr/bin/env python3

import os, re

def fixMySubtitles(mediaFolderPath):
    files = os.listdir(mediaFolderPath)

    for file in files:
        movie = ""
        subtitle = ""
        if re.search(r'\.ass', file):
            subtitle = re.search(r'(.*).ass', file).group(1)
            movie = [movie for movie in files if subtitle in movie]
            movie = [movie for movie in movie if '.mkv' in movie]
            movie = "".join(movie)
            try:
                movie = re.search(r'(.*).mkv', movie).group(1)
            except:
                print("Movie not found error for subtitle '%s'" % (file))
            newPath = os.path.join(mediaFolderPath, "%s.ass" % (movie))
            if movie:
                os.rename(os.path.join(mediaFolderPath, file), newPath)

    print("I have fixed your subtitles!")

fixMySubtitles("/Users/My_User/Desktop/test")

希望这会有所帮助!

答案 1 :(得分:0)

这是我的解决方案...为变量inDir分配文件路径-我认为-为D:\Downloads\Media\Anime

import os
import re
from glob import glob

inDir = "D:\Downloads\Media\Anime"

lst = glob(os.path.join(inDir, r"*.ass"))
for filepath in lst:
    parent, filename = os.path.split(filepath)
    episode = re.findall(r"Banana Fish - (\d+).ass", filename)[0]
    newName = "[PuyaSubs!] Banana Fish - {} [1080p][2ECC9207].ass".format(episode)
    os.chdir(parent)
    os.rename(filename, newName)