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

2010-02-12 Thread kevin parks

hi

I am new territory here and not even sure where to start poking around  
other than the os module some.


Essentially i need to do something like a shell script for batch  
processing gobs of files. I am trying to use a command line tool (sox,  
an open source sound file converter that runs from the unix command  
line) and I don't want to edit the command line, run the job, edit the  
command line, etc over and over again for hundreds of small files.


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...


so if i had

kp/flute/ST

 kp8/flute/ST/foo01.aif
 kp8/flute/ST/foo02.aif
 kp8/flute/ST/foo03.aif
 kp8/flute/ST/foo04.aif
 kp8/flute/ST/foo05.aif

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


kp/flute/STout/

 kp/flute/STout/foo01.wav
 kp/flute/STout/foo02.wav
 kp/flute/STout/foo03.wav
 kp/flute/STout/foo04.wav
 kp/flute/STout/foo05.wav

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. A typical command line call i need to make to, say,  
batch convert files from one sample rate and bit depth to another  
would look like so:


% 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?


cheers,

kevin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-02-12 Thread Alan Gauld


kevin parks k...@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
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor