运行 带有输入文件的脚本 python

running a script with an input file in python

我有一个 Python 脚本,它必须针对多种情况执行 Fortran 代码。该代码需要一个作为字符串输入的输入文件。

伪代码

input_file='input_'+case+'.in' os.system('./a.out input_file)'

这里的 case 是一个变量,因为它对于每个测试用例都是不同的。 这不起作用,因为我需要将字符串 (input_file) 本质上转换为文件名。

因此您使用新的 string.format 约定来传递变量:

input_file="input_{0}.in".format(case)

os.system=("./a.out {0}".format(input_file))