Random music using Python

I want to automate playing random music as shown in Python Automation Scripts You Should Know. However, it wasn’t what I’d expected. When I finally make it work, it only play 1 fixed song, no loop.

How do I make it pay randomly, loop, and play all the music in that folder? And if possible, how to write a script that play all music from more than 1 folder without opening the folder at random?

Windows version

  1. If you want to play all music randomly in the folder → just need to shuffle order of the music and loop to it
songs = os.listdir(music_dir)
random.shuffle(songs)
for song_name in songs :
    song_path = os.path.join(music_dir, song_name)
    print(f'playing... {song_name}')
    os.startfile(song_path)
  1. if you want to play the song in specific folder → create answer 1 as a function and have argument with folder path
def randomly_music(folder_path: str) :
    songs = os.listdir(folder_path)
    random.shuffle(songs)
    print(f'playing music from {folder_path}')
    for song_name in songs :
        song_path = os.path.join(music_dir, song_name)
        print(f'playing... {song_name}')
        os.startfile(song_path)

folders_path = [path1, path2 ... ]
for folder_path in folders_path :
   randomly_music(folder_path)
1 Like
  1. Only play 1 random music. It doesn’t play all the music in the music_dir, after each music ended.
  2. I was thinking of it play all the music in a range of folders, but at random.

I planned to listen to a range of random music from either 1 specific folder or ranges of folders, automatically while studying.

Another thing that would help greatly is not inputting the hard-coded folder path. Currently, my variable os.path.join and pathlib are not working. Maybe someone here can explain why is that.

Hello this is Gulshan Negi
Well, to play all music in one folder you can execute below code:

#!/usr/local/Microsoft/WindowsApps/python
import random, os
music_dirs = [‘C:/Users/XXXX/Music/XXX’, ‘C:/Users/XXXX/Music/YYY’]
songs =
for music_dir in music_dirs:
songs.extend(os.listdir(music_dir))

infinite loop to play music randomly

while True:
# get a random song from the list of songs
song = random.choice(songs)
# get the directory of the song
music_dir = None
for dir in music_dirs:
if song in os.listdir(dir):
music_dir = dir
break
# print the song name
print(song)
# play the song
os.startfile(os.path.join(music_dir, song))

Thanks

I’m sure I got something wrong, because nothing happened. There is no error too. Lastly, may I ask what with songs = []

As for the infinite loop, sorry if I don’t get it. Why is the directory path set to None? Was this part simply an extension to the previous main code above (as it lack the shebang; and import random, os)?

#!/usr/local/Microsoft/WindowsApps/python
import random, os
folder_A = “C:/Users/XXX/Music/YYY”
folder_B= “C:/Users/XXX/Music/ZZZ”
music_dirs = [folder_A, folder_B]
songs =
for music_dir in music_dirs:
songs.extend(os.listdir(music_dir))

It seems like you’re almost there with your Python script for playing random music. To make it play randomly, loop, and play all the music in the folder, you need a few adjustments. Here’s an updated version of your script that achieves this:

#!/usr/local/Microsoft/WindowsApps/python
import random, os
import time

music_dir = 'C:/Users/XXXX/Music/XXX'
songs = os.listdir(music_dir)

while True:
    random.shuffle(songs)  # Shuffle the list of songs
    for song in songs:
        print("Playing:", song)
        os.startfile(os.path.join(music_dir, song))
        time.sleep(10)  # Adjust the sleep time between songs if needed

This script shuffles the list of songs and plays each song in the shuffled order, creating a loop of random music playback. The time.sleep() function adds a delay between songs. You can adjust the duration according to your preference.

For playing music from multiple folders without manually opening the folders, you can modify the script as follows:

#!/usr/local/Microsoft/WindowsApps/python
import random, os
import time

music_dirs = ['C:/Users/XXXX/Music/XXX', 'C:/Users/XXXX/Music/YYY']
songs = []

for music_dir in music_dirs:
    songs.extend(os.listdir(music_dir))

while True:
    random.shuffle(songs)
    for song in songs:
        print("Playing:", song)
        for music_dir in music_dirs:
            if song in os.listdir(music_dir):
                os.startfile(os.path.join(music_dir, song))
                break
        time.sleep(10)

This version of the script combines songs from multiple folders and plays them randomly, without needing to manually open the folders.

As a side note, while your interest in Spotify Premium is acknowledged, please note that the provided Python script is for playing locally stored music files. To use Spotify’s services, you would typically need to use their official APIs and adhere to their terms of use.

Feel free to modify the script further to suit your preferences and needs. Happy coding!

It seems there might be a syntax error in the provided code snippet. Let’s correct it:

pythonCopy code

import os
from pathlib import Path

path = '/home/user_nameXXX/Music'
song_folder_name = 'Song_folder_name'

# Using os.path.join
music_dir_os = os.path.join(path, song_folder_name)

# Using pathlib.Path
music_dir_pathlib = [Path](https://www.galvanizedsteelflanges.com/)(path, song_folder_name)

In the corrected code, I’ve introduced a variable song_folder_name to store the name of the song folder separately. This helps avoid hard-coding the folder name directly within the os.path.join and pathlib.Path functions.

Make sure to use the correct variable names when referencing the folder path components. The corrected code should work without issues, allowing you to dynamically create the path to your song folder.