서브 프로세스를 사용하여 실행 파일을 실행하고 통신을 사용하여 서브 프로세스를 출력합니다. 마지막으로, 통신 내용을 파일에 씁니다. 정확한 코드는 아래에 표시됩니다.통신을 사용하여 서브 프로세스의 stdout을 파일로 작성
run = subprocess.Popen(['executable'], stdout=subprocess.PIPE)
output = run.communicate()[0]
logfile = open('run.log', 'a')
logfile.write(output)
logfile.close()
위의 과정에서 로그 파일은 실행이 끝날 때 작성됩니다. 그러나 실행 파일이 실행 중일 때 출력을 로그에 기록하는 방법이 있습니까?
이logfile = ('run.log', 'w')
run = subprocess.Popen(['executable'], stdout = logfile)
run.wait()
logfile.close()
첫 번째 줄이 만들어 쓰기에 대한 run.log
및 stdout
은 실행 파일과 로그 파일에 직접 기록 : 나는이에 대한 해결책을 찾은 것 같아요
with open("run.log","a") as f:
run = subprocess.Popen(['executable'], stdout=f)
이 내용은 [docs] (https://docs.python.org/3/library/subprocess.html#frequently-used-arguments)에서 직접 설명합니다. "* stdin *, * stdout * 및 * stderr * 유효한 값은'PIPE','DEVNULL', 기존의 파일 기술자 (양의 정수), 기존의 파일 객체,'None'입니다. 실행 된 프로그램의 표준 입력, 표준 출력, 표준 오류 파일 핸들을 지정합니다. – abarnert