[Tutor] Responding to a request for input from a MS-DOS program

2006-10-11 Thread Jeffrey Kennedy
Title: Responding to a request for input from a MS-DOS program







Hi,
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 (which it will subsequently do some analysis on). It asks two more questions (each requiring me to type '1' followed by 'enter') before it does the analysis; I don't need to capture any output (it will save the analysis in files which I can access later).
I'm new to Python. I have run some simple DOS commands (copy, delete), using os.system, but need some pointers on how to do this task. 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 (the files are named 1.sav, 2.sav, ... 100.sav).
I run Python 2.4 on Windows XP and (needless to say) I'm new to Python. >From web searches, it seems that a variation on popen might do what I want, but I'd like a pointer to some examples I can play around with.
Thanks for any help you can give.

Jeff



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Responding to a request for input from a MS-DOS program

2006-10-12 Thread Jeffrey Kennedy
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 "", 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 "", 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...
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor