[英]Adding ID3 Tags in Mutagen by Looping Through a Directory
(就编码而言,我是一个绝对的初学者,这是我有史以来的第一个项目。)
我正在尝试在Windows 10上使用Mutagen遍历目录,并为遇到的每个文件添加ID3标记跟踪号。 不幸的是,Mutagen似乎无法识别上述文件。 到目前为止,这是我的代码:
import os
import mutagen
import mutagen.id3
from mutagen.id3 import ID3
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
files = os.listdir(r'C:\Users\Kurt\Music\Music\Brad Paisley\python test')
count = 0
for file in files:
path = file
try:
tag = EasyID3(path)
except:
tag = mutagen.File(path, easy=True)
tag.add_tags()
tag['tracknumber'] = count + 1
tag.save(v2_version=3)
file.save()
count = count + 1
从本质上讲,这与我在Google上搜索过的各种其他内容融合在一起,但是在运行它时给了我一个巨大的错误,我将在这里发布前几行:
Traceback (most recent call last):
File "C:\Users\Kurt\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mutagen\_util.py", line 235, in _openfile
fileobj = open(filename, "rb+" if writable else "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'americansaturdaynight 01- American Saturday Night - Copy.mp3'
就像我说的那样,我是一个绝对的初学者,所以我不太了解其中的大部分含义,尽管这似乎意味着我选择的目录(或目录中的文件?)不存在,这是非常重要的。 谁能帮我这个忙吗?
您的问题在于路径os.listdir()
仅列出文件/目录,而不列出它们的路径,因此,除非您恰巧从指定路径执行脚本,否则Python将无法找到该文件。
您始终可以将原始路径定义为:
source_dir = r"C:\Users\Kurt\Music\Music\Brad Paisley\python test"
for name in os.listdir(source_dir): # iterate over all files/directories in source_dir
if name[-4:].lower() != ".mp3": # ignore non-mp3 files
continue
path = os.path.join(source_dir, name) # build the whole file path
# you can also check with os.path.isfile(path) to make sure it's a file you're processing
# etc. (your mutagen update logic)
您可能还需要查看glob.glob()
以使用已应用的扩展名过滤器模式查找直接目录列表,因此您不必亲自进行手动检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.