开发者

Subprocess.communicate() returns char at a time rather than lines

I am running Python 2.7.2 on Windows and trying to automate a number of command line tools using subprocess as follows:

p = subprocess.Popen(mip4 + " status", bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
stdout, stderr = p.communicate()
for line in stdout:
    print line

Rather than producing a full line of output, the print statement prints a single character followed by a newline.

However, if I do:

print p.communicate() 

the开发者_运维问答 full output is printed correctly.

I've tried all the bufsize options to no avail.


If your aim to get output of the command , you should use check_output.

In [120]: for x in subprocess.check_output(["/bin/sh", "-c", "ls -   a"]).split('\n'):
                print x
        .....:     
        .....:     
        .
        ..
        cluster.py
        database1.py
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜