Getting realtime output using subprocess

前端 未结 18 2794
执笔经年
执笔经年 2020-11-22 10:12

I am trying to write a wrapper script for a command line program (svnadmin verify) that will display a nice progress indicator for the operation. This requires me to be abl

18条回答
  •  野性不改
    2020-11-22 11:10

    Few answers suggesting python 3.x or pthon 2.x , Below code will work for both.

     p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,)
        stdout = []
        while True:
            line = p.stdout.readline()
            if not isinstance(line, (str)):
                line = line.decode('utf-8')
            stdout.append(line)
            print (line)
            if (line == '' and p.poll() != None):
                break
    

提交回复
热议问题