標籤: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全部隨機播放電腦裡的音樂