Executing DOS (yes, DOS) program from within Python?

Peter Hansen peter at engcorp.com
Mon Oct 6 21:20:57 EDT 2003


Ben Fairbank wrote:
> 
> I am preparing a Python program that has to call a DOS statistical
> program that asks the user to give an input file name and an output
> file name from the console, it then reads input, processes the data,
> and writes the output file (quaint, no?).  I want to run it without
> keyboard input, but rather using Python statements to specify the same
> input and output files every time it runs.  (It is a compiled Pascal
> program and changing its I/O and recompiling is not really an option.)
> I have looked through Hammond and Robinson without finding any
> suggestions; can a reader tell me where the proper Python way to do
> that is documented?

This isn't a DOS-specific or Windows-specific issue actually, so that 
might be why the Win32 book didn't help so much.  I think you are 
looking for the likes of os.popen() and the popen2 module.

Is it possible, however, that the program can take input redirected
from a file?  If instead of typing the commands you can create a
file containing the exact same text, including CR/LF, and then execute
the program with redirected input, as in the following example, then
you might find it easier to write the commands out to a file and then
execute your program with os.system():

   yourprog.exe <inputfile

If that works, then os.system("yourprog.exe <inputfile") would work,
provided your script creates "inputfile" ahead of time.

-Peter




More information about the Python-list mailing list