[Tutor] Replacing keyboard input to EXE file

eShopping etrade.griffiths at dsl.pipex.com
Thu Jun 11 14:06:14 CEST 2009


>Hi
>
>I have a legacy (FORTRAN) program called POLY.EXE which asks the 
>user interactively for three inputs (file names) from the 
>keyboard.  I would like to run this program in batch and tried to 
>replace the interactive prompts with file names stored in a separate 
>file using this Python script:
>
>import subprocess
>
>try:
>    poly_file = file("poly_files.txt","r")
>except:
>    print "Could not open input file"
>else:
>    x = subprocess.Popen(args="poly.exe", stdin=poly_file)
>    x.wait()
>
>poly_files.txt contains the three file names as follows:
>
>polyin.dat
>polyout.dat
>polyout.plt
>
>On execution, I get these error messages
>Traceback (most recent call last):
>  File "C:/Projects/Active/Alun/test_poly.py", line 4, in -toplevel-
>    x = subprocess.Popen(args="poly.exe", stdin=poly_file)
>  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
>
>So, it looks like something is wrong with the way I am redirecting 
>stdin, but I have no idea what the problem is.  Any suggestions 
>gratefully received
>
>
>I've not done much with subprocess, but I know you can use the 
>subprocess.PIPE to redirect stdin/stdout.
>
>This may be of some help too,
> 
><http://www.oreillynet.com/onlamp/blog/2007/08/pymotw_subprocess_1.html>http://www.oreillynet.com/onlamp/blog/2007/08/pymotw_subprocess_1.html
>
>-Wayne

Wayne

I did try using stdin but got the same problem.  I also hacked the 
following from the website you suggested:

import subprocess

x = subprocess.Popen(args="poly.exe",stdin=subprocess.PIPE)

for item in ["polyin.dat", "polyout.dat", "polyout.plt"]:
     x.stdin.write('%s\n' % item)

but got the same error message

Best regards

Alun


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090611/df987524/attachment.htm>


More information about the Tutor mailing list