我需要有关此程序的一些反馈

问题描述

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脚本中播放它们,该怎么办?请随意编辑我的代码

解决方法

在Linux find中,这是一个非常有用的命令,用于在目录中搜索和查找文件。

here开始,您可以使用较少的参数来做类似的事情-

import subprocess

searchPath = '/'
songName = 'Some Cool Song.mp3'
subprocess.Popen(('find',searchPath,'-type','f','-name',songName)

现在,我的建议是不要使用根目录进行搜索。由于可能要花费一些时间,因此不必要。而是让用户选择一些目录来查找。然后递归在这些目录中运行find命令。这样可以减少时间并提高功能。