2013-01-01 34 views
1

我想創建一個簡單的python腳本,查看文件夾和子文件夾,並創建一個包含mp3文件夾名稱的播放列表。但到目前爲止,我只遇到了Python的模塊,在Linux上工作或我無法弄清楚如何安裝它們(pymad)..Python模塊創建音樂播放列表(windows)

這只是我的android手機所以想通過m3u格式應該做..我不關心任何其他元數據而不是mp3文件的名稱。

+0

http://code.google.com/p/fappy/吧? (但是 - 「(窗口)」如何與Android設備相關)? –

+1

'os.walk'和'os.path.splitext'有什麼問題? – mmgp

+0

@mmgp根本沒有問題 - 這實際上是個好主意!我只是不知道os.walk – Norfeldt

回答

2

其實我只是看着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.. 
1

我寫了一些代碼,會根據你的條件會返回所有嵌套播放列表的候選人名單:

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('.') 
+0

你應該使用'os.walk'。另外,'gimp3.png'是一個mp3文件嗎? 'x.count'在這裏確實是錯誤的,使用'os.path.splitext'並且可能將它與基本的文件標識結合起來。 – mmgp

+0

謝謝,但我已經解決了這個問題(請參閱我的文章)。但是,謝謝你的建議 – Norfeldt

+0

啊,是的,忘記os.walk。你是對的,伯爵是這樣做的。感謝您的反饋! – bdombro