Home | Stats | Downloads | Scripts | Wiki
in fact, you might say that you are the best service I ever quit.
-- 89110     Add quote.

Random Albums with Python

2008-09-03 00:51:33.124830 by Michael 0 Comments
Tags: python programming

While setting up my new music server today I had a small issue to take care of, setting up play lists. While I do like having tracks play randomly most of my music is meant to be listened to as a complete album, Dark Side of the Moon, for example.

Enter python, in less than 25 lines of code I came up with a solution. The below script parses through my music directory, shuffles the albums and then creates a play list file.

#!/usr/bin/env python

import commands, os
from random import *

cmd = "find /export/home/music -type d"

dirs = commands.getoutput(cmd).split("\n")

f = open("/usr/local/etc/ices-playlist.txt", "w")

for dir in dirs:
        cmd = "find '%s' -maxdepth 1 -type f -name '*.mp3' -print | sort" %dir
        output = commands.getoutput(cmd)


os.system("sed -i '/^$/d' /usr/local/etc/ices-playlist.txt")