[Tutor] Responding to a request for input from a MS-DOS program
Jeffrey Kennedy
AJeffrey at ntu.edu.sg
Fri Oct 13 05:19:31 CEST 2006
Hi Alan,
I think I might be trying to walk before I can crawl; for benefits of
other newbies, I'll summarise lessons learned so far.
Firstly, I spent ages trying to find out why the Python documentation
syntax for Popen, using stdin=PIPE, didn't work:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
p1=subprocess.Popen("c:/temp/enter.bat", stdin = PIPE)
NameError: name 'PIPE' is not defined
Turns out it needs to be specified as stdin=subprocess.PIPE. Then spent
even longer trying to figure out why this command:
>>> p=subprocess.Popen("c:/temp/enter.bat", stdin=subprocess.PIPE)
gave this error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
p=subprocess.Popen("c:/temp/enter.bat", stdin=subprocess.PIPE)
File "C:\Python24\lib\subprocess.py", line 533, in __init__
(p2cread, p2cwrite,
File "C:\Python24\lib\subprocess.py", line 607, in _get_handles
c2pwrite = self._make_inheritable(c2pwrite)
File "C:\Python24\lib\subprocess.py", line 634, in _make_inheritable
DUPLICATE_SAME_ACCESS)
WindowsError: [Errno 6] The handle is invalid
(and stdout gives TypeError: an integer is required).
Google eventually helped me find out that it's a problem with the way
the IDLE Python GUI treats stdin and stdout rather than a problem in the
code (which is based on an example in your excellent tutorial).
I created a two-line DOS batch file to emulate what I'm trying to do.
The file (saved as 'enter.bat') is:
SET /P = Type '1' then press 'Enter'
SET /P = Type '2' then press 'Enter'
After typing the following lines into the Python command line editor,
enter.bat runs and takes over the command prompt (waiting for the first
input):
import subprocess
cmd='C:/temp/enter.bat'
p1=subprocess.Popen(cmd, stdin = subprocess.PIPE)
OUTPUT:
>>> p1=subprocess.Popen(cmd, stdin = subprocess.PIPE)
>>>
C:\Python24>SET /P = Type '1' then press 'Enter'
Type '1' then press 'Enter'
I then tried the following:
subprocess.Popen(cmd, stdin = subprocess.PIPE).stdin.write('1\n')
This seems to work - the entry seems to be accepted, but the second
prompt from enter.bat now takes over the command prompt:
OUTPUT:
>>> subprocess.Popen(cmd, stdin = subprocess.PIPE).stdin.write('1\n')
>>> C:\Python24>SET /P = Type '1' then press 'Enter'
Type '1' then press 'Enter'
C:\Python24>SET /P = Type '2' then press 'Enter'
Type '2' then press 'Enter'
Am I on the right track? I suspect I need to use checks to make sure the
enter.bat program has responded before sending the input, but I'm trying
to get the basic commands right first.
Thanks again,
Jeff
Hi Jeff,
> I want to use Python to run an old MS-DOS program that doesn't
> accept arguments when launched. I have to open the program first,
> then it will ask me for the name of another program
....
> I want to bypass the human input, because I have over
> 100 files I want processed, and I can use Python to iterate through
> each file in turn
You need to look at the subprocess module and the examples that
replace the older popen functions.
You will find more info including examples of both popen and
subporocess in my tutorial under the OS topic in the
Applications Section
Since you are new to this you might want to read the whole topic,
or for more directed help scroll down to the Manipulating Processes
heading. Unfortunately I just noticed I don't give any excample of
writing to a process(in response to a prompt) which is something I
should fix...
More information about the Tutor
mailing list