Run batch files in Windows XP

Peter Hansen peter at engcorp.com
Mon Jul 25 15:26:22 EDT 2005


Ernesto wrote:
> The issue is I haven't done very much batch programming.  I need to
> prompt the user for input and each batch file is in a different
> directory.  How do I change directories and prompt for user input?

Ah, now you're talking things where Python, while not strictly required, 
can start to make things easier. :-)

To run external things like batch files, you import the "os" module and 
use the system() function:

import os
os.system('somebatchfile.bat')

Since you're prompting the user for input first, presumably the input is 
passed to the batch file in some way, like so:

input = raw_input('Enter your incredibly useful info now:')

("raw_input" is a builtin... good to learn about as many of those as you 
can handle by reading the docs here: 
http://docs.python.org/lib/built-in-funcs.html .  Some of those are 
absolutely not important to you at this early stage, others are.)

Then with the input you've collected, you could pass it to the batch 
file like so:

os.system('myfile.bat %s' % input)

The %s and % together result in whatever text the user entered being 
added on the command line for the call to myfile.bat, exactly as though 
it had been executed manually with the same input.

-Peter



More information about the Python-list mailing list