1

I am using pexpect with python to create a program that allows a user to interact with a FORTRAN program through a website. From the FORTRAN program I am receive the error:

open: Permission denied apparent state: unit 4 named subsat.out.55 last format: list io lately writing sequential formatted external IO 55

when I attempt to:

p = pexpect.spawn(myFortranProgram,[],5)
p.logfile_read = sys.stdout
p.expect("(.*)")
p.sendline("55")

From what I understand, I am likely sending the 55 to the wrong input unit. How do I correctly send input to a FORTRAN program using pexpect in Python?

Thank You.


Edit: When p.sendline's parameter is empty (e.g. p.sendline()) or only contains spaces, the program proceeds as expected. In sending non-space values to a FORTRAN program, do I need to specify the input format somehow?

Mat Kelly
  • 2,327
  • 7
  • 29
  • 51
  • That's not much to go on. If you want more help, you will need to provide more data about your programs and how you're using them, especially how the Fortran program expects to get data. – GreenMatt Jul 29 '11 at 17:39
  • A very important point about pexepect: It only runs under linux. There is a windows port but I ran into many problems getting it to run on my box. – Spencer Rathbun Jul 29 '11 at 18:13

1 Answers1

1

The pexpect module is something I'd not used before, but could be useful to me, so I tried this.

Edit:

I've not been able to duplicate the error you're reporting. Looking at this error leads me to believe that it has something to do with reading from a file, which may be a result of other issues. From what I've seen, this isn't what pexpect is designed to handle directly; however, you may be able to make it work with a pipe, like the example in my original answer, below.

I'm having no problem sending data to Fortran's I/O stream 5 (stdin). I created a Fortran program called regurgitate which issues a " Your entry? " prompt, then gets a line of input from the user on I/O stream 5, then prints it back out. The following code works with that program:

import pexpect
child = pexpect.spawn('./regurgitate')
child.setecho(False)
ndx = child.expect('.*Your entry?.*')
child.sendline('42')
child.expect([pexpect.EOF])
print child.before
child.close()

The output is simply:

42

Exactly what I expected. However, if my Fortran program says something different (such as "Your input?"), the pexpect just hangs or times out.

Original suggestion:

Maybe this pexpect.run() sample will help you. At least it seems to run my regurgitate program (a simple Fortran program that accepts an input and then prints it out):

import pexpect
out = pexpect.run('/bin/bash -c "/bin/cat forty-two | ./regurgitate"')
print out

The output was:

Your entry?
42

Where regurgitate prints out a "Your entry?" prompt and the forty-two file contains "42" (without quotes in both cases).

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
  • The issue seems to be with the newline being sent. If I child.sendline('42'), I get the above error. child.sendline(), child.sendline(" ") or any parameter with only white space for content does not produce the error. I also tried following child.send('42'), which works but does not "submit" the content because of now trailing CR/LF, with child.sendline() but that again bring up the above error. Do you know anything else I can try to get over this issue? – Mat Kelly Aug 03 '11 at 16:42