#!/usr/bin/python

# This piece of code creates playlists in all subdirectories of the current directory
# Place this in the root music dir of your Sansa MP3 player, run and enjoy browsing by directory
# Requires Python installed (http://www.python.org/download/releases/2.4.4/)
#
# by GDR! (http://gdr.geekhood.net/)

import os

directories = ['.']

while len(directories)>0:
	directory = directories.pop()
	
	try:
		dirs = os.listdir(directory)
	except:
		print "Odmowa dostepu: ", directory
		
	plname = os.path.split(directory)[-1]
	if plname == ".":
		plname = "RootDir"
	
	plname += ".m3u"
	print plname
	plname = os.path.join(directory, plname)
	playlist = open(plname, 'w')
	playlist.write("#EXTM3U\n")
	inlist = []
	
	for name in dirs:
		fullpath = os.path.join(directory,name)
#		print fullpath
		if os.path.isfile(fullpath):
			if fullpath.split(".")[-1] in ['mp3', 'wma']:
 				inlist.append(name)
				pass
				
		elif os.path.isdir(fullpath):
			directories.append(fullpath)  # It's a directory, store it.

	inlist.sort()
	for name in inlist:
		playlist.write(name + '\n')
	playlist.close()
