gpt4 book ai didi

运行 C 程序的 Python 脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:19:51 25 4
gpt4 key购买 nike

<分区>

我有一个 C/C++ 程序,它接受一组参数并将一组输出显示到命令行(用于我的研究)。

我想编写一个 Python 脚本来针对不同的输入多次运行该程序,并将输出写入文件。我计划使用详尽的输入来运行该程序。

但是,我没有任何使用 Python 编写脚本或编程的经验。所以,我想知道我是否可以获得一些从哪里开始的指示。

举个例子,我想写一个脚本来做:

./program -flag1 [val1] -flag2 [val2] -arg1 -arg2 -arg3 ...
Append the output to Output.txt
./program -flag1 [val1] -flag2 [val2] -arg1 -arg2 -arg4 ...
Append the output to Output.txt
./program -flag1 [val1] -flag2 [val2] -arg1 -arg2 -arg5 ...
Append the output to Output.txt
...
...
./program -flag1 [val1] -flag2 [val2] -arg1000 -arg1000 -arg1000 ...
Append the output to Output.txt

编辑:我通过命令行 bash 在 Linux 上运行该程序。

EDIT2 SLN:仅供将来可能是初学者的其他人引用,做类似的事情,解决方案如下所示。我去掉了所有只影响我的情况的部分。

import subprocess
from subprocess import Popen, PIPE

for commands in listArgs:

# Build command through for loop in listArgs.
# Details are omitted.
cmd = ["./program", "-flag1", "val1", "-flag2", "val2", "-arg1", "-arg2", ... ]

# Open/Create the output file
outFile = open('/path/to/file/Output.txt', 'a+')

result = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out = result.stdout.read()

outFile.write(out)
outFile.close()

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com