Hi Everyone - I am a complete and utter Python newbie (as of today, honestly) -- am interested in expanding my programming horizons beyond bash scripting and thought Python would be a nice match for me.
To start, I thought I may try re-writing some of my bash scripts in Python as a learning tool for me ... and the first one I wanted to talkle was a script that converts .flac audio files into .mp3 files ... basic idea is I supply a sourceDirectory and targetDirectory and then recursively convert the source file tree into an identical target file tree filled with mp3 files. I'm sure this has been done before (by those much wiser than me) but I figured I can learn something as I go ... for what I've accomplished so far, it seems pretty ugly! But I'm learning ... Anyhow, I think I got the basics down but I had a thought: can I thread this program to utilize all of my cores? And if so, how? Right now, the lame audio encoder is only hitting one core ... I could do all this faster if I could pass a variable that says: open 2 or 4 threads instead. Here is what I've been working on so far -- would appreciate any insight you may have. Thanks, Damon #!/usr/bin/env python import os import sys import fnmatch from os import system fileList = [] rootDir = sys.argv[1] targetDir = sys.argv[2] def shell_quote(s): """Quote and escape the given string (if necessary) for inclusion in a shell command""" return "\"%s\"" % s.replace('"', '\\"') def _mkdir(newdir): """works the way a good mkdir should :) - already exists, silently complete - regular file in the way, raise an exception - parent directory(ies) does not exist, make them as well http://code.activestate.com/recipes/82465/ """ if os.path.isdir(newdir): pass elif os.path.isfile(newdir): raise OSError("a file with the same name as the desired " \ "dir, '%s', already exists." % newdir) else: head, tail = os.path.split(newdir) if head and not os.path.isdir(head): _mkdir(head) #print "_mkdir %s" % repr(newdir) if tail: os.mkdir(newdir) # get all the flac files and directory structures for dirpath, subFolders, files in os.walk(rootDir): for file in files: if fnmatch.fnmatch(file, '*.flac'): flacFileInfo = [os.path.join(dirpath,file),dirpath+"/",file,dirpath.lstrip(rootDir)+"/"] fileList.append(flacFileInfo) # create new directory structure and mp3 files for sourceFile,dir,flacfile,strip in fileList: mp3File = shell_quote(targetDir + strip + flacfile.strip('.flac') + ".mp3") mp3FileDir = targetDir + strip sourceFile = shell_quote(sourceFile) _mkdir(mp3FileDir) flacCommand = "flac --decode --stdout --silent " + sourceFile + " | lame -V4 --slient - " + mp3File system(flacCommand) _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor