艺术家组织歌曲| python,eyed3
我正在尝试编写代码来组织+40G的音乐,我想按艺术家组织,到目前为止,我已经能够获得艺术家信息,但我正在为每个艺术家创建一个目录,让同一艺术家的歌曲进入同一个目录,而不是为每首歌曲创建一个单独的目录艺术家组织歌曲| python,eyed3,python,directory,Python,Directory,我正在尝试编写代码来组织+40G的音乐,我想按艺术家组织,到目前为止,我已经能够获得艺术家信息,但我正在为每个艺术家创建一个目录,让同一艺术家的歌曲进入同一个目录,而不是为每首歌曲创建一个单独的目录 import os #imports os functions import eyed3 #imports eyed3 functions root_folder = '/Users/ntoscano/desktop/mp3-organizer' files = os.listdir(root_
import os #imports os functions
import eyed3 #imports eyed3 functions
root_folder = '/Users/ntoscano/desktop/mp3-organizer'
files = os.listdir(root_folder) #lists all files in specified directory
if not files[1].endswith('.mp3'):
pass #if the file does not end with ".mp3" it does not load it into eyed3
for file_name in files:
#if file_name.endswith('.mp3'): continue #if file ends with ".mp3" it continues onto the next line
abs_location = '%s/%s' % (root_folder, file_name)
song_info = eyed3.load(abs_location) #loads each file into eyed3 and assignes the return value to song_info
if song_info is None:
print 'Skippig %s' % abs_location
continue
os.mkdir(os.path.expanduser('~/Desktop/mp3-organizer/%s')) % song_info.tag.artist
print song_info
print song_info.tag.artist
这是我到目前为止所知道的,但是它被打断了,第19行总是给我一个错误
Nicolass-MacBook-Air:mp3-organizer ntoscano$ python eyeD3test.py
Skippig /Users/ntoscano/desktop/mp3-organizer/.DS_Store
Traceback (most recent call last):
File "eyeD3test.py", line 19, in <module>
os.mkdir(os.path.expanduser('~/Desktop/mp3-organizer/%s')) % song_info.tag.artist
TypeError: unsupported operand type(s) for %: 'NoneType' and 'unicode'
我对编码还不熟悉,所以我确信这是一个简单的错误,但我只是不明白如何获得一个以艺术家信息为名称的目录。任何帮助都是值得的问题只是括号放错了地方。与此相反:
os.mkdir(os.path.expanduser('~/Desktop/mp3-organizer/%s')) % song_info.tag.artist
这样做:
os.mkdir(os.path.expanduser('~/Desktop/mp3-organizer/%s' % song_info.tag.artist))
如果将其分解为多个部分,则更容易看到:
expanded = os.path.expanduser('~/Desktop/mp3-organizer/%s')
dir = os.mkdir(expanded)
formatted = dir % song_info.tag.artist
因此,您正在创建一个名为/Users/ntoscano/Desktop/mp3-organizer/%s的目录,该目录将返回None,然后您将执行None%song\u info.tag.artist,因此%不支持非类型和unicode的错误
在这种情况下,在expanduser之前还是之后进行格式化并不重要,但必须在mkdir之前进行格式化
顺便说一句,尽管这是合法的,但对于%-格式“~/Desktop/mp3-organizer/%s”%song_-info.tag.artist',使用单个值而不是元组通常是个坏主意。一般来说,使用现代{}格式而不是~/Desktop/mp3-organizer/{}格式是一个更好的主意。而且使用os.path而不是字符串操作更好,所以这些问题一开始都不会出现
另外,我注意到您在某些情况下使用的是手动扩展的root_文件夹,而在其他情况下使用的是expanduser。您可能希望在这方面保持一致,否则,当您尝试在另一台机器上使用它时,~不是/Users/ntoscano,它就会崩溃。而且,即使OSX允许您在某些情况下错误地获取路径名,您也应该尽可能地正确获取路径名
总而言之:
root_folder = os.path.expanduser('~/Desktop/mp3-organizer')
files = os.listdir(root_folder) #lists all files in specified directory
for file_name in files:
#if file_name.endswith('.mp3'): continue #if file ends with ".mp3" it continues onto the next line
abs_location = os.path.join(root_folder, file_name)
song_info = eyed3.load(abs_location) #loads each file into eyed3 and assignes the return value to song_info
if song_info is None:
print 'Skippig %s' % abs_location
continue
os.mkdir(os.path.join(root_folder, song_info.tag.artist))
print song_info
print song_info.tag.artist
问题只是括号放错了地方。与此相反:
os.mkdir(os.path.expanduser('~/Desktop/mp3-organizer/%s')) % song_info.tag.artist
这样做:
os.mkdir(os.path.expanduser('~/Desktop/mp3-organizer/%s' % song_info.tag.artist))
如果将其分解为多个部分,则更容易看到:
expanded = os.path.expanduser('~/Desktop/mp3-organizer/%s')
dir = os.mkdir(expanded)
formatted = dir % song_info.tag.artist
因此,您正在创建一个名为/Users/ntoscano/Desktop/mp3-organizer/%s的目录,该目录将返回None,然后您将执行None%song\u info.tag.artist,因此%不支持非类型和unicode的错误
在这种情况下,在expanduser之前还是之后进行格式化并不重要,但必须在mkdir之前进行格式化
顺便说一句,尽管这是合法的,但对于%-格式“~/Desktop/mp3-organizer/%s”%song_-info.tag.artist',使用单个值而不是元组通常是个坏主意。一般来说,使用现代{}格式而不是~/Desktop/mp3-organizer/{}格式是一个更好的主意。而且使用os.path而不是字符串操作更好,所以这些问题一开始都不会出现
另外,我注意到您在某些情况下使用的是手动扩展的root_文件夹,而在其他情况下使用的是expanduser。您可能希望在这方面保持一致,否则,当您尝试在另一台机器上使用它时,~不是/Users/ntoscano,它就会崩溃。而且,即使OSX允许您在某些情况下错误地获取路径名,您也应该尽可能地正确获取路径名
总而言之:
root_folder = os.path.expanduser('~/Desktop/mp3-organizer')
files = os.listdir(root_folder) #lists all files in specified directory
for file_name in files:
#if file_name.endswith('.mp3'): continue #if file ends with ".mp3" it continues onto the next line
abs_location = os.path.join(root_folder, file_name)
song_info = eyed3.load(abs_location) #loads each file into eyed3 and assignes the return value to song_info
if song_info is None:
print 'Skippig %s' % abs_location
continue
os.mkdir(os.path.join(root_folder, song_info.tag.artist))
print song_info
print song_info.tag.artist
我只是在做类似的工作。我用图书馆扫描文件,可能对你有用。祝你好运,我也在做类似的工作。我用图书馆扫描文件,可能对你有用。祝你好运