首页 > 解决方案 > 根据元数据重命名 mp3 文件

问题描述

我正在尝试使用其元数据中的标题重命名我的所有音乐文件。我正在使用Pydroid3

import os
from mutagen.easyid3 import EasyID3

def rename_mp3_files(location):
    for root, dirs, files in os.walk(location):
        for m_file in files:
            if m_file.endswith(".mp3"):
                old_filepath = os.path.join(root, m_file)

                new_filename = "{}.mp3".format(old_filepath["title"])
                new_filepath = os.path.join(root, new_filename)

                os.rename(old_filepath, new_filepath)

                print (old_filepath["title"])

location = "/storage/emulated/0/Music"
rename_mp3_files(location)

但它给出了错误

  File "<string>", line 18, in <module>
  File "<string>", line 10, in rename_mp3_files
TypeError: string indices must be integers

谁能告诉我我在哪里做错了我是菜鸟如果我犯了错误或遗漏了问题中的某些内容,请原谅我。

标签: pythonmp3file-renamebatch-rename

解决方案


这个:

old_filepath = os.path.join(root, m_file)

创建str然后你做:

print(old_filepath["title"])

失败是因为str_variable[n]从字符串中提取第 n 个字符的方法 -"title"第一个字符没有意义。尝试更换:

print(old_filepath["title"])

使用:

print(old_filepath)

推荐阅读