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.

1 Answers1

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")
.......
Michael
  • 5,095
  • 2
  • 13
  • 35