Constantly print Subprocess output while process is running

后端 未结 13 1050
庸人自扰
庸人自扰 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:38

    In Python >= 3.5 using subprocess.run works for me:

    import subprocess
    
    cmd = 'echo foo; sleep 1; echo foo; sleep 2; echo foo'
    subprocess.run(cmd, shell=True)
    

    (getting the output during execution also works without shell=True) https://docs.python.org/3/library/subprocess.html#subprocess.run

提交回复
热议问题