python - 根据元数据重命名 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
谁能告诉我我在哪里做错了我是菜鸟如果我犯了错误或遗漏了问题中的某些内容,请原谅我。
解决方案
这个:
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)
推荐阅读
- ios - SwiftUI 中 State 后绑定更新,触发 2 次更新
- swift - 有没有办法在 watchOS 上运行 AudioKit?
- amazon-s3 - AWS S3 Post 对象拒绝额外的表单字段
- scala - 使用 Play (Scala) 读取 JSON 时,如何根据另一个字段的值选择字段的类型?
- javascript - 如何创建一个函数以便我可以控制组的大小?所以我可以选择每组是 2 人还是 3 人
- r - 在R中通过字符串匹配删除一行,无论它是否存在?
- angular - 在Angular的另一个异步函数中访问函数的结果值
- javascript - 如果通过条件,我如何返回 Json
- sql - 选择一组重复数据
- python - 尝试使用 buildozer 构建应用程序但返回错误,有人知道怎么了吗?