passing command line arguments to executable

Joshua bantonj at gmail.com
Sun Apr 4 21:47:01 EDT 2010


On 4/3/10 12:09 PM, mcanjo wrote:
> I have an executable (I don't have access to the source code) that
> processes some data. I double click on the icon and a Command prompt
> window pops up. The program asks me for the input file, I hit enter,
> and then it asks me for and output filename, I hit enter a second time
> and it goes off and does its thing and when it is finished running the
> Command Prompt goes away and I have my new output file in the same
> directory as my executable and input file. I would like to be able to
> batch process a group of files. I thought about using "os.spawnv()" in
> a loop and at each iteration of the loop passing in the file in and
> out names but that didn't work. Does anyone have any ideas?

I've done this a couple times on Windows. There are a few ways to do it, 
but I've found the easiest way is to use subprocess.check_call(). If you 
need to build the call with variables I find it cleaner to build the 
string before calling check_call(). Below is an example.

callString = 'program.exe -x ' + variable
returnMessage = subprocess.check_call(callString, shell=True)

This works for programs that take arguments, I don't know how it will 
work with the program you have that asks for input in an interactive 
way. Does it accept arguments when you first run it? Not sure how to do 
it otherwise.

-Josh B.



More information about the Python-list mailing list