我想在 Fortran 中调用 python 代码,并使用 system() 命令将 python 中生成的值传输到 Fortran。然而,这不起作用。 这是我的 Fortran 代码:
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
这是我要调用的Python代码:
def generate_a_number(a):
return a
generate_a_number(2)
我得到了打印结果:
FORTRAN: BLOCKING
0
2
FORTRAN: EXIT
Process returned 0 (0x0) execution time : 0.141 s
Press any key to continue.
返回值始终为0。不知道是否可以使用命令system()来传输数据。如果没有,如何从python获取数据。 任何建议将不胜感激。
最佳答案
函数system()
仅返回执行状态。要从 Python 脚本获取结果,您可以将其写入文件:
call system("python python_number.py > tmp.txt")
open(11, file="tmp.txt")
.......
关于python - 无法使用 s = system() 将值从 python 传输到 Fortran 中定义的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69231322/