通过目录循环在 Mutagen 中添加 ID3 标签
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
这基本上是我用谷歌搜索过的其他各种东西拼凑而成的,但是当我 运行 它时它给了我一个巨大的错误,我将 post 这里的前几行:
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()
仅列出 files/directories 而不是它们的路径,因此,除非您恰好从指定路径执行脚本,否则 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()
应用扩展过滤器模式的直接目录列表,这样您就不必自己进行手动检查。
(我在编码方面绝对是初学者,这是我的第一个项目。)
我正在尝试在 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
这基本上是我用谷歌搜索过的其他各种东西拼凑而成的,但是当我 运行 它时它给了我一个巨大的错误,我将 post 这里的前几行:
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()
仅列出 files/directories 而不是它们的路径,因此,除非您恰好从指定路径执行脚本,否则 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()
应用扩展过滤器模式的直接目录列表,这样您就不必自己进行手动检查。