Constantly print Subprocess output while process is running

后端 未结 13 1066
庸人自扰
庸人自扰 2020-11-22 06:51

To launch programs from my Python-scripts, I\'m using the following method:

def execute(command):
    process = subprocess.Popen(command, shell=True, stdout=s         


        
13条回答
  •  半阙折子戏
    2020-11-22 07:43

    There is actually a really simple way to do this when you just want to print the output:

    import subprocess
    import sys
    
    def execute(command):
        subprocess.check_call(command, stdout=sys.stdout, stderr=subprocess.STDOUT)
    

    Here we're simply pointing the subprocess to our own stdout, and using existing succeed or exception api.

提交回复
热议问题