Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

python to call or start a fortran a.out

29 views
Skip to first unread message

Chet Buell

unread,
Aug 21, 2017, 12:31:48 PM8/21/17
to
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.




Grant Edwards

unread,
Aug 21, 2017, 12:52:01 PM8/21/17
to
https://docs.python.org/2/library/commands.html:

Deprecated since version 2.6: The commands module has been removed
in Python 3. Use the subprocess module instead.

The subprocess module provides more powerful facilities for
spawning new processes and retrieving their results. Using the
subprocess module is preferable to using the commands module.

https://docs.python.org/2/library/subprocess.html#popen-constructor:


--
Grant Edwards grant.b.edwards Yow! I'm encased in the
at lining of a pure pork
gmail.com sausage!!

Peter Otten

unread,
Aug 21, 2017, 1:21:27 PM8/21/17
to
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.

Rick Johnson

unread,
Aug 22, 2017, 7:44:28 AM8/22/17
to
> TITLE: python to call or start a fortran a.out

Please do consider the title of your posts more carefully.

The title you chose here was unfortunate, as it will not
provide an intuitive link between the problem and the
solution, therefore, it is unlikely that someone with the
same _general_ problem, will benefit from this exchange in
the future.

The _general_ problem here had nothing to with Fortran, much
less a specific file or executable named "a.out". So a
better title would have been: "How to run an executable from
Python", or: "How to spawn a subprocess from Python and
capture the result". Or even: "How to poke my OS with a
sharp stick, using Python". Perhaps the last example was a
bit too generalized, i admit, but you get the point. :-)

A good template for composing questions on internet forums
is as follows:

MESSAGE_TITLE: "A concise, generalization of the problem here..."

MESSAGE_BODY: " An elaborate and specific explanation of the problem here..."

Mentioning "Fortran" and "a.out" in the body of the message,
would be fine.

One of the happy-accidents[1] of composing thoughtful
queries, is that, many times you will happen upon the answer
to your own question during the composition process, and
even if you don't, at least you will have a much better
chance of receiving prompt and quality responses by
following this template. So either way, it's a win-win.

[1] Think "Bob Ross" here. As his most important
contribution to society was that he taught us all a very
important lesson. Namely, that all accidents can be "happy
accidents". An eternal optimist, he was...
0 new messages