Only getting the first 6 lines

Cecil Westerhof Cecil at decebal.nl
Fri Oct 2 07:11:46 EDT 2015


On Friday  2 Oct 2015 09:37 CEST, Peter Otten wrote:

> Cecil Westerhof wrote:
>
>> I want to get the first 6 lines of ps output. For this I use:
>> ========================================================================
>> from subprocess import check_output
>>
>> ps_command = ('ps', '-eo',
>> 'user,pid,pcpu,pmem,stat,start,time,cmd', '--sort') message =
>> '\n'.join(check_output(ps_command +
>> ('-%cpu',)).decode("utf-8").splitlines()[0:6])
>> ========================================================================
>>
>> It works, but does not look very efficient. Is there a better way
>> to do this?
>
> Efficiency be damned, readability counts ;)

The second is more important as the first, but if you can have both …
:-)

I already made a function for it. See my other response.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list