나는 인수 집합을 취하여 일련의 결과를 명령 행에 표시하는 C/C++ 프로그램을 가지고있다.C 프로그램을 실행하는 Python 스크립트
다른 입력에 대해이 프로그램을 여러 번 실행하고 출력을 파일에 기록하는 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
편집을 :
예를 들어, 나는 일을위한 스크립트를 작성하고 싶습니다 나는 명령 줄 떠들썩한 파티를 통해 리눅스에서 프로그램을 실행하고 있습니다.
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()
https://docs.python.org/3/tutorial/ – MattDMo
파이썬의 서브 프로세스는 아마 트릭을 잘 할 것입니다. . . 그 말로는 'bash'와 같은 도구가 아마도이 작업에 더 적합 할 것입니다. – mgilson
어떤 플랫폼입니까? Windows? 맥? 리눅스? –