运行 Fortran 代码,输入文件来自 python

Running fortran code with imput file from python

我感兴趣的是让我的 python 代码 运行 这个 unix 命令行:

./code < file

代码是 fortran,但我认为我应该避免使用 F2PY。第一,fortran 代码的输出是文件,我还有其他代码,它们的工作是读取它们。第二,我想让它尽可能简单。该文件只是我的 Fortran 代码可以读取的常规输入文件。

现代的做法是简单地使用 subprocess.run:

import subprocess
with open("file") as f:
    subprocess.run("path_to_script", stdin=f)

请注意,我只是传递文件描述符,而不是在 Python 内读取它。