유닉스와 윈도우에서 똑같이 잘 작동하는 Python 2 *의 getstatusoutput()
함수에 대한 위대한 대체품을 발견했습니다. 그러나, 나는 output
이 구성되는 방식에 문제가 있다고 생각합니다. 출력의 마지막 줄만 반환하지만 그 이유는 알 수 없습니다. 어떤 도움이라도 굉장합니다.Python getstatusoutput 대체가 전체 출력을 반환하지 않음
def getstatusoutput(cmd):
"""Return (status, output) of executing cmd in a shell."""
"""This new implementation should work on all platforms."""
import subprocess
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
output = "".join(pipe.stdout.readlines())
sts = pipe.returncode
if sts is None: sts = 0
return sts, output
try 블록 내에서'subprocess.check_call '을 사용하지 않는 이유는 무엇입니까? 'except' 부분에서 리턴 코드 (0이 아닌 경우)를 얻을 수 있습니다. – wim
반환 코드가 0 인 경우에도 출력을 원한다면 어떻게해야합니까? – MFB