external binary reading standard in

Martin v. Löwis loewis at informatik.hu-berlin.de
Wed Jul 31 09:02:18 EDT 2002


Markus O Kaukonen <markus.kaukonen at iki.fi> writes:

> How to execute a external (binary) program  reading from standard input
> in a python code ?
> For example :  /home/mok/my_prog.bin < my_input.txt
> 
> My system is Linux and the binary is written originally in fortran 90
> (if that should matter).

You want the standard input to be from a file, and you want to get the
standard output in the Python script? I believe you need a
pipe/fork/exec combination for that. It works roughly like that

fdin, fdout = os.pipe()
pid = os.fork()
if pid==0:
  # child
  os.dup2(fdout, 1)
  fdin = os.open('my_input.txt',os.O_RDONLY)
  os.dup2(fdin, 0)
  os.execv('/home/mok/my_prog.bin', ['my_prog'])

# parent
fromchild = os.fdopen(fdin, "r")

HTH,
Martin



More information about the Python-list mailing list