How to play random mp3 from a folder in Python
This tutorial is about how to play random mp3 from a folder in Python. Python contains a lot of Predefined modules. Python has a module that is the random module by using the random module to play random mp3 from a folder. So keep reading it to see how we can do it…
The following are constrains to play an mp3 from a folder:
- The mp3 audio folder path must be specified to open the mp3 audio on the computer.
- By using random.choice() method to select a particular mp3 audio present in the folder.
- All the mp3 audio must be stored in the .py file location to start the mp3 audio using the OS module otherwise you must change to mp3 audio folder location using change directory to play the mp3 audio.
Importing Random Module:
So, let us have a look at importing the random module:
import random
Importing random module in .py file
Importing OS Module:
So, let us have a look at importing the OS module:
import random import os
Importing os module in .py file
Folder Details:
The folder contains a lot of mp3 audios by using random function a particular mp3 audio is selected and played by using OS module
#-----------------Inside the folder----------- audio1.mp3 audio2.mp3 audio3.mp3
Example to play random mp3 from a folder:
import os import random path="C:\\Users\\sairajesh\\Desktop\\audios" files=os.listdir(path) d=random.choice(files) os.startfile(d)
output:
Hence, the audio file will be playing on your default music player once you try.
#random audio file will be played on default player
Explanation:
- First, you select the path of the folder where the mp3 audios are present like->c\\user\\folder33
- By using the listdir() method store all the files present in the folder
- By using random.choice() method to select a particular mp3 audio and os.startfile() method to play the mp3 audio
OS.Start file() method:
The os.startfile() method will be used to run the file present in the folder directly on the default opener of the file. Here the mp3 audios file will be played on VLC player or media player, etc.
there is some small change in your code that u specified above.
there is error in last line “os.startfile(d)” as “Exception has occurred: FileNotFoundError
[WinError 2] The system cannot find the file specified: ‘myfile.mp3′”
solution for this –
import os
import random
path=”F:\\Songs\\”
files=os.listdir(path)
d=random.choice(files)
os.startfile(‘F:\\Songs\\’ + d)
its not working same error is showing again
sorry for the inconvenience make sure your python (.py) file is saved in that folder where the MP3 files are located, to avoid those errors. if you again facing any issues please share the error that you are facing to solve the problem regards k Venkata Kalyan (Author)
its not working same error, rubbish site.
sorry for the inconvenience
make sure your python (.py) file is saved in that folder where the MP3 files are located, to avoid those errors.
if you again facing any issues please share the error that you are facing to solve the problem
regards
k Venkata Kalyan (Author)
doesnt work file not found error and before you say put it in the same folder i cannot do that because of how my script is going to wrok
I Have the solution of error !!
Just replace :-
os.startfile(d)
with :-
os.startfile(f”# path were is music is located#{d}”)
in my case it was :-
os.startfile(f”C:/Users/Kazi Fahim/Music/mp3/{d}”)
music_files = os.listdir(“D:\\Music\\5SOS\\”)
selected_file = random.choice(music_files)
os.startfile(os.path.join(“D:\\Music\\5SOS\\”,selected_file))
This worked for me for random songs
Can anyone tell me how to play all the songs from a folder using the same method?? in python only?
I tries a lot but couldn’t do it..
Please help if anyone knows
import random
import os
#get your path every time or it wont work
path = “C:\\Users\\Fantascey\\Documents\\Music”
files = os.listdir(path)
print(files)
#this part here chooses the file at random
folder=random.choice(files)
print(folder)
songspath = (f’C:/Users/Fantascey/Documents/Music/{folder}’)
songlist = os.listdir(songspath)
print(songlist)
#this part here chooses the song itself at random
song = random.choice(songlist)
print(song)
chosensong = (f’C:/Users/Fantascey/Documents/Music/{folder}/{song}’)
os.startfile(chosensong)
#all this works without the print but with it you can see what is playing
#note that if you put this into a while loop you can keep playing music
#the while loop will need a clock for to only play a new song after each
#other song is done playing
everything will work if you will change the last line as
os.startfile(os.path.join(path,files[d]))