我想創建一個簡單的python腳本,查看文件夾和子文件夾,並創建一個包含mp3文件夾名稱的播放列表。但到目前爲止,我只遇到了Python的模塊,在Linux上工作或我無法弄清楚如何安裝它們(pymad)..Python模塊創建音樂播放列表(windows)
這只是我的android手機所以想通過m3u格式應該做..我不關心任何其他元數據而不是mp3文件的名稱。
我想創建一個簡單的python腳本,查看文件夾和子文件夾,並創建一個包含mp3文件夾名稱的播放列表。但到目前爲止,我只遇到了Python的模塊,在Linux上工作或我無法弄清楚如何安裝它們(pymad)..Python模塊創建音樂播放列表(windows)
這只是我的android手機所以想通過m3u格式應該做..我不關心任何其他元數據而不是mp3文件的名稱。
其實我只是看着http://en.wikipedia.org/wiki/M3U並認爲這是很容易寫的m3u文件...應該能夠簡單的Python寫做文本file`
這裏是我的解決方案
import os
import glob
dir = os.getcwd()
for (path, subdirs, files) in os.walk(dir):
os.chdir(path)
if glob.glob("*.mp3") != []:
_m3u = open(os.path.split(path)[1] + ".m3u" , "w")
for song in glob.glob("*.mp3"):
_m3u.write(song + "\n")
_m3u.close()
os.chdir(dir) # Not really needed..
我寫了一些代碼,會根據你的條件會返回所有嵌套播放列表的候選人名單:
import os
#Input: A path to a folder
#Output: List containing paths to all of the nested folders of path
def getNestedFolderList(path):
rv = [path]
ls = os.listdir(path)
if not ls:
return rv
for item in ls:
itemPath = os.path.join(path,item)
if os.path.isdir(itemPath):
rv= rv+getNestedFolderList(itemPath)
return rv
#Input: A path to a folder
#Output: (folderName,path,mp3s) if the folder contains mp3s. Else None
def getFolderPlaylist(path):
mp3s = []
ls = os.listdir(path)
for item in ls:
if item.count('mp3'):
mp3s.append(item)
if len(mp3s) > 0:
folderName = os.path.basename(path)
return (folderName,path,mp3s)
else:
return None
#Input: A path to a folder
#Output: List of all candidate playlists
def getFolderPlaylists(path):
rv = []
nestedFolderList = getNestedFolderList(path)
for folderPath in nestedFolderList:
folderPlaylist = getFolderPlaylist(folderPath)
if folderPlaylist:
rv.append(folderPlaylist)
return rv
print getFolderPlaylists('.')
http://code.google.com/p/fappy/吧? (但是 - 「(窗口)」如何與Android設備相關)? –
'os.walk'和'os.path.splitext'有什麼問題? – mmgp
@mmgp根本沒有問題 - 這實際上是個好主意!我只是不知道os.walk – Norfeldt