命令行python3 myCode.py source_folder target_folder
我需要创建一个文件夹,该文件夹包含按专辑年份分类的.mp3文件。
源文件夹是我所有.mp3
所在的位置。在目标文件夹中,必须创建与专辑年份相对应的子文件夹。
找到歌曲后,曲目将被复制到相应的子文件夹中。如果歌曲没有年份,它将被复制到“ Uncategorized
”文件夹中。
如果目标文件夹存在,它将被删除,然后将被重新创建(我不理解这一部分)
import os #create path
import sys # arguments
from tinytag import TinyTag # reading music metadata of MP3
import shutil # copy files from the path to another path
target_folder = sys.argv[2]
if not os.path.exists(target_folder):
os.mkdir(sys.argv[2])
folder_path = "./" + sys.argv[1] # get the first command-line argument
folder_uncategorized = target_folder +'/Uncategorized' # get the second command-line argument
if not os.path.exists(folder_uncategorized):
os.mkdir(folder_uncategorized) # create folder uncategorized
for path, dirs, files in os.walk(folder_path):
for filename in files:
tag = TinyTag.get(path+'/'+filename)
#if the song's year isn't mention in its meta information
if str(tag.year) == 'None':
shutil.copy(path+'/'+filename,folder_uncategorized+'/')
else:
#if the song's year is mention in its meta information
folder_year = target_folder + '/'+ str(tag.year)
if not os.path.exists(folder_year):
os.mkdir(folder_year)
shutil.copy(path+'/'+filename,folder_year+'/')
问题是我有70 .mp3
,运行代码时,仅处理23 .mp3
。 tinytag
Traceback <most recent call>:
File : "C:Users\user\myCode.py", line 21 in <module>
tag = TinyTag.get (path+'/'+filename)
File : "C:\Users\user\AppData\......\tinytag.py", line 127 in get
parser_class = cls.get_parser_for filename <filename.exceptin=True)
File : "C:\Users\user\AppData\.......\tinytag.py" line 118, in get_parser_for_filename
raise TinyTagException <'No Tag reader found to support filetype!'>
tinytag.tinytag.TinyTagException : No tag reader found to support filetype!