Reputation: 85
How can I store only the last line returned from a subprocess?
In particular, I call a subprocess from python and it returns a lot of output lines. The subprocess terminates after some unpredictable (and possibly very long) time and I need to process only the last line from STDOUT.
Can I somehow avoid the storing of the whole output and just wait for the last one?
Upvotes: 4
Views: 1758
Reputation: 91029
You'll have to process (and discard) every line before reading the last one.
So you could do something like
line = None
for line in proc.stdout:
pass
# now line is either the last line or None.
if line is not None:
process(line)
Upvotes: 1