2

I want to call python code in Fortran and transfer values generated in python to Fortran using system() command. However, it doesn't work. here is my fortran code:

program SystemTest
  character(len=256) :: cmd
  integer :: py
  print *, "FORTRAN: BLOCKING"
  py = system("python python_number.py")
  print *, py
  py = 2
  print *, py
  print *, "FORTRAN: EXIT"
end program SystemTest

and this is the python code I want to call:

def generate_a_number(a):
    return a
generate_a_number(2)

and I got this printed results:

 FORTRAN: BLOCKING
           0
           2
 FORTRAN: EXIT

Process returned 0 (0x0)   execution time : 0.141 s
Press any key to continue.

the returned value is always 0. I wonder whether the command system() can be used to transfer data. if not, how to obtain data from python. any suggestion will be appreaciated.

2
  • 2
    system is not a standard Fortran function, so please tell us about your compiler. Commented Sep 18, 2021 at 5:31
  • @francescalus, that right, better to use standard function Fortran2008: execute_command_line(). Commented Sep 18, 2021 at 18:11

1 Answer 1

2

The function system() returns only the status of execution. To get the result from Python script, you can write it to a file:

call system("python python_number.py > tmp.txt")
open(11, file="tmp.txt")
.......
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.