[Tutor] batch file processing w/ python using cmd line executable?

Alan Gauld alan.gauld at btinternet.com
Fri Feb 12 21:00:42 CET 2010

"kevin parks" <kp8 at mac.com> wrote 

> I wonder if it is possible to use python to call sox and have it do  
> os.mkdir, process all the input files in a particular directory and  
> put the converted files into the directory it made with mkdir...

Of course. What do you perceive to be the problem?
This would just be a standard loop or os.walk call.

> The script would call sox repeatedly and create a new dir with a  
> converted file for each found in the original folder. like so:

Presumably a new dir for each dir found?
Thus mirroring the source structure? Thats easily done with 
os.walk and would be a good place to start writing the script.

Calling sox can be done using the subprocess module.

> what makes this especially hairy is that sox is a monster an typically  
> needs a crazy number of arguments, though these would be the same for  
> the whole batch. 

So store them in a list and apply them to each call. 
subprocess makes this easy.

> % sox -V3 -D -S St.01.aif -b16  kp/flute/STout/St.01.wav rate -s -v  
> 44100
> Is there away to do this in python, by just pointing it to a whole dir  
> of files and say "do it" to all of these?

I'd check out os.walk and subprocess.

Both are discussed in the Using the OS topic in my tutorial.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list