Avatar

Michael's Blog

Home | Gallery | Stats | Downloads | Links | Scripts | Fuel Stats | Wiki | RSS
Quote:
When CPanel screws up it goes all the way.
-- Dylan     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")
shuffle(dirs)

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)
        f.write(output)
        f.write("\n")

f.close()

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