问题描述
import os
import random
from playsound import playsound
from win10toast import ToastNotifier
def play_song():
song_list = list()
for dirpath,dirname,filename in os.walk('C:/Users/Praneeth Ravuri/Downloads/Songs'):
for files in filename:
if files.endswith('.mp3'):
song_list.append(files)
for i in song_list:
index_mp3 = i.index('.mp3')
print(i[0:index_mp3])
choice = input("Select a song number ('S') or listen to a random song ('R') or ('Q') to quit: ").lower()
print("\n")
if choice == 's':
choice_number = int(input("Enter a song number: "))
if choice_number in range(len(song_list)):
print("Current Playing Song: ",song_list[choice_number-1])
toaster = ToastNotifier()
toaster.show_toast("Currently Playing",song_list[choice_number - 1],duration=5)
playsound(song_list[choice_number-1])
play_song()
elif choice == 'r':
random_song = random.choice(song_list)
print("Current Playing Song: ",random_song)
toaster = ToastNotifier()
toaster.show_toast("Currently Playing",random_song,duration=5)
playsound(random_song)
play_song()
elif choice == 'q':
print("You Chose To Quit")
print("Thank you for listening to Praneeth's Music Player!")
else:
print("Please enter a valid input")
play_song()
print("Praneeth's Ravuri Music Player")
play_song()
当我使用“ playsound”模块时,歌曲应与Python脚本位于同一文件夹中。如果要在计算机上搜索不同目录和文件夹中的mp3文件并在mp3脚本中播放它们,该怎么办?请随意编辑我的代码