针对Python 3,有些事情发生了变化,但我的代码可以在Mac上运行。
@yask是正确的,你应该检查不存在的值,这是我的示例:
复制并粘贴,只需调整路径以满足您的需求,并可在文件路径循环中使用。
"""PlaceHolder."""
import re
from os import path as ospath
from eyed3 import id3
current_home = ospath.expanduser('~')
file_path = ospath.join(current_home,
'Music',
'iTunes',
'iTunes Media',
'Music',
'Aerosmith',
'Big Ones',
'01 Walk On Water.mp3',
)
def read_id3_artist(audio_file):
"""Module to read MP3 Meta Tags.
Accepts Path like object only.
"""
filename = audio_file
tag = id3.Tag()
tag.parse(filename)
artist = tag.artist
title = tag.title
track_path = tag.file_info.name
if artist is not None:
artist.encode()
artistz = re.sub(u'`', u"'", artist)
else:
artistz = 'Not Listed'
if title is not None:
title.encode()
titlez = re.sub(u'`', u"'", title)
else:
titlez = 'Not Listed'
if track_path is not None:
track_path.encode()
track_pathz = re.sub(u'`', u"'", track_path)
else:
track_pathz = ('Not Listed, and you have an the worst luck, '
'because this is/should not possible.')
try:
if artist is not None and title is not None and track_path is not None:
print('Artist: "{}"'.format(artistz))
print('Track : "{}"'.format(titlez))
print('Path : "{}"'.format(track_pathz))
except Exception as e:
raise e
read_id3_artist(file_path)
audiofile.tag
是None
... - jonrsharpeNone
- 阅读文档,找出原因。 - jonrsharpe