Python全部隨機播放電腦裡的音樂

來源:互聯網
上載者:User

標籤:article   return   text   ida   span   [1]   檔案   file   imp   

就是找到硬碟中全部的MP3檔案和wma檔案。再隨機開啟當中的一個。

import os,randomdisk=[‘D‘,‘E‘,‘F‘,‘G‘,‘H‘] def search_file(filename,search_path,pathsep=os.pathsep):    for path in search_path.split(pathsep):        candidate = os.path.join(path,filename)        if os.path.isfile(candidate):            return os.path.abspath(candidate)def random_play():    music=[]    found=False    for i in range(0,5):        for dirpath, dirnames, filenames in os.walk(disk[i]+‘:/‘):            for filename in filenames:                if os.path.splitext(filename)[1] == ‘.mp3‘:                    filepath = os.path.join(dirpath, filename)                    if os.path.getsize(filepath)>1048576:#這裡是為了防止開啟一些非音樂音頻。比方遊戲配音                        music.append(filepath)                        found=True    if not found:        for dirpath, dirnames, filenames in os.walk(‘C:/Users‘):            for filename in filenames:                if os.path.splitext(filename)[1] == ‘.mp3‘:                    filepath = os.path.join(dirpath, filename)                    if os.path.getsize(filepath)>1048576:                        music.append(filepath)                        found=True    if not found:        for i in range(0,5):            for dirpath, dirnames, filenames in os.walk(disk[i]+‘:/‘):                for filename in filenames:                    if os.path.splitext(filename)[1] == ‘.wma‘:                        filepath = os.path.join(dirpath, filename)                        if os.path.getsize(filepath)>1048576:                            music.append(filepath)                            found=True    if not found:        for dirpath, dirnames, filenames in os.walk(‘C:/Users‘):            for filename in filenames:                if os.path.splitext(filename)[1] == ‘.wma‘:                    filepath = os.path.join(dirpath, filename)                    if os.path.getsize(filepath)>1048576:                        music.append(filepath)                        found=True    random_music=random.choice(music)    os.startfile(random_music)    if not found:        print‘沒有找到音頻檔案‘if __name__==‘__main__‘:    random_play()


Python全部隨機播放電腦裡的音樂

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.