Get exit code and stderr from subprocess call

后端 未结 5 2219
逝去的感伤
逝去的感伤 2020-12-14 05:07

I read up on the functions provided by subprocess - call, check_call, check_output, and understand how each works and differs in functionality from one another. I am curren

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 05:48

    I had a similar requirement, and the following worked for me:

        try:
            with open ("vtcstderr.out", "w") as file:
                rawOutput = subprocess.check_output(
                    command,
                    stderr=file,
                    shell=True
                )
        except subprocess.CalledProcessError as error:
            # this is the stdout
            rawOutput = error.output
    
        with open ("vtcstderr.out", "r") as file:
            # this is the stderr
            errorLines = file.readlines()
    
    

提交回复
热议问题