Read streaming input from subprocess.communicate()

后端 未结 7 912
攒了一身酷
攒了一身酷 2020-11-21 05:53

I\'m using Python\'s subprocess.communicate() to read stdout from a process that runs for about a minute.

How can I print out each line of that process

相关标签:
7条回答
  • 2020-11-21 06:22
    myCommand="ls -l"
    cmd=myCommand.split()
    # "universal newline support" This will cause to interpret \n, \r\n and \r     equally, each as a newline.
    p = subprocess.Popen(cmd, stderr=subprocess.PIPE, universal_newlines=True)
    while True:    
        print(p.stderr.readline().rstrip('\r\n'))
    
    0 讨论(0)
提交回复
热议问题