Schorsch Schorsch - 6 months ago 40
Python Question

Python subprocess not executing program correctly

I'm trying to execute a Fortran program in a shell from Python.
I'm using

to call the Fortran program and pass arguments (two input files are required). This is how I do it:

COMMAND = '/path/to/FORTRAN/program << !\n' + INPUT_FILE1 + '\n' + INPUT_FILE2 +'\n!'
return =, shell=True)

Unfortunately, the Fortran program exits with:

? FORTRAN Runtime Error:
? Attempt to read past end of file
? READ(UNIT=22,...

This is not an issue with the input files or the Fortran program. When I execute this in my shell:

$>/path/to/FORTRAN/program << !

everything works as expected (no
Attempt to read past end of file

I don't have access to the Fortran source, so I cannot check what is going on in there. I think it has to do with the way I'm calling the program through
- I just can't figure out what.

I'm using Python 2.6.6.

Quick explanation: The Fortran program will prompt for two input files, but it doesn't recognize them as command-line options (they cannot be provided with the call to the program). If you want to run it from - say a shell script (batch mode) - you need to capture the return command to execute the command, but also provide the two files. The
<< !
opens a "list" for lack of a better term. The following commands are handed to the program which waits with its execution until
is typed.

I have used this approach multiple times successfully (different Fortran code), and with this one, using a C shell script (not Python).


You could try "\r\n" (tty newline):

from subprocess import Popen, PIPE

p = Popen(["/path/to/FORTRAN/program"], stdin=PIPE,
          bufsize=1, close_fds=True)
rc = p.returncode

with/without "\r\n" at the end.