首页 > 解决方案 > Truncate stdout of subprocess.run() without shell=True

问题描述

I'm running a binary executable from Python using the subprocess.run() command and the command spits out about 20MB of text data to stdout. I'm only interested in the first few lines of the output and loading the entire output of the command into the memory takes a very long time (about 10 seconds).

I would like to read stdout of the command up the 10th line and then truncate all other output. What I would like to achieve is the equivalend of running command | head (which is super fast), but I have the shell=False set which does not allow the use of pipes.

Is there any way I can truncate the output of stdout to just a few lines/bytes without loading it all into memory? I already tried the bufsize= parameter, but it had no effect.

标签: pythonsubprocess

解决方案


When using subprocess.Popen you can have access to the subprocess's stdout and read as many lines as you want

p = subprocess.Popen(cmd, stdout=subprocess.PIPE)

lines_to_read = 10

for i in range(lines_to_read):
    print(p.stdout.readline())

推荐阅读