Getting realtime output using subprocess

前端 未结 18 2925
执笔经年
执笔经年 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:12

    Found this "plug-and-play" function here. Worked like a charm!

    import subprocess
    
    def myrun(cmd):
        """from http://blog.kagesenshi.org/2008/02/teeing-python-subprocesspopen-output.html
        """
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        stdout = []
        while True:
            line = p.stdout.readline()
            stdout.append(line)
            print line,
            if line == '' and p.poll() != None:
                break
        return ''.join(stdout)
    

提交回复
热议问题