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

B

Ben Fairbank

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?

Thanks,

Ben Fairbank
 
P

Peter Hansen

Ben said:
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
 
C

cmkl

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?

Thanks,

Ben Fairbank

Is it possible to run your application from commandline or
from within a batch file WITHOUT manual user interaction?
In this case you can start the application with os.system or
os.popen2.
I prefer "winprocess.py" in the \Lib\site-packages\win32\demos
folder (but you need to install win32all)

Maybe it is not possible to run the application without user
interaction, so you can give Mick Trent's "process.py" a try.
(see http://starship.python.net/crew/tmick/)
This module has a ProcessProxy class for handling this issue.

"SendKeys" is another option to emulate keybord events on the
Windows desktop and send it to an application window. Not a
nice solution for this kind of problem, but a working one.
(http://www.rutherfurd.net/python/sendkeys/index.html)

Carl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top