Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Only getting the first 6 lines Date: Fri, 02 Oct 2015 13:11:46 +0200 Organization: Decebal Computing Lines: 28 Message-ID: <87pp0xplsd.fsf@Equus.decebal.nl> References: <87y4fmp7xi.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="8192"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+buAK339zec5szVJB9dMggdRazgIbx1+U=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:hhwSl1tJTHgr6NilQ3fdIV5pFZQ= sha1:vAjqnZP/Isml3tKB0ATEhgOtjqs= X-Homepage: http://www.decebal.nl/ Xref: csiph.com comp.lang.python:97331 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