'python subprocess can’t print last line with PIPE

p =Popen('cmd.exe',shell=True,stdin=PIPE,stdout=PIPE,bufsize=0)

def run():
    global p
    while True:
        line = p.stdout.read(1)
        if not line:  #空则跳出
            break
        sys.stdout.write(line.decode("GBK"))
    sys.stdout.write("look up!!! EXIT ===")   #跳出

when I run, i got

Microsoft Windows [Version 10.0.22000.613]
(c) Microsoft Corporation. All rights reserved.

But when I use stdout=sys.stdout,I got

Microsoft Windows [Version 10.0.22000.613]
(c) Microsoft Corporation. All rights reserved.

C:\Windows>

why I can't get last line with PIPE,i want to know if process end or timeout by last line



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source