python to call or start a fortran a.out

Peter Otten __peter__ at web.de
Mon Aug 21 13:20:53 EDT 2017


Chet Buell wrote:

> Need some help with updating python to call or start a fortran a.out
> executable
> 
> The problem I am having is I have an old Fortran based model that I need
> to run, in the past the fortran was triggered through the following
> python code:
> 
> #run fortran
> x = commands.getoutput(path+'/a.out')
> 
> Since the commands.getoutput has been depreciated it will not run on the
> newly built server that will now host this model. I have manually
> confirmed that the compiled a.out does run and produces the files, but
> the problem I am having is in triggering it to run with python.
> 
> The answer is probably so easy it is obviously being overlooked, but I
> can not seem to find it.  Any suggestions or help would be greatly
> appreciated.

I don't see how the documentation could be made more explicit than

https://docs.python.org/2/library/commands.html

"""
Note In Python 3.x [...] getstatusoutput() and getoutput() have been moved 
to the subprocess module.
"""

There is also the 2to3 tool that may help you with the mechanical part of 
the migration from Python 2.7 to 3.x. Example:

$ cat runfortran.py
#!/usr/bin/env python
import commands

x = commands.getoutput(path+'/a.out')
print x
$ 2to3-3.6 -w runfortran.py
RefactoringTool: Skipping optional fixer: buffer
[...]
RefactoringTool: Files that were modified:
RefactoringTool: runfortran.py
$ cat runfortran.py
#!/usr/bin/env python
import subprocess

x = subprocess.getoutput(path+'/a.out')
print(x)
$ 

Personally I would avoid the shell and capture stdout/err in separate 
strings with

p = subprocess.run(
    [os.path.join(path, "a.out")], 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
    universal_newlines=True
)
print(p.stdout)

or similar.




More information about the Python-list mailing list