Path: csiph.com!news.mixmin.net!news2.arglkargh.de!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none:': 0.05; 'python3': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'subject:getting': 0.07; 'cmd': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:= 70': 0.10; 'def': 0.13; "skip:' 30": 0.15; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subprocess': 0.16; 'wrote:': 0.16; "skip:' 40": 0.22; 'bit': 0.23; 'skip:( 40': 0.23; 'import': 0.24; 'sort': 0.25; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; "skip:' 10": 0.28; 'skip:u 20': 0.28; '100000': 0.29; 'code': 0.30; 'run': 0.33; 'this?': 0.34; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'lines': 0.36; 'faster': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'skip:o 20': 0.38; 'does': 0.39; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'total': 0.62; 'per': 0.62; 'counts': 0.81; '215': 0.84; 'cecil': 0.84; 'westerhof': 0.84; 'subject:Only': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Only getting the first 6 lines Date: Fri, 02 Oct 2015 09:37:54 +0200 Organization: None References: <87y4fmp7xi.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8265.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1443771487 news.xs4all.nl 23821 [2001:888:2000:d::a6]:59121 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97327 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 ;) With that in mind here's a little code cleanup: import subprocess def text_head(text, n): return "\n".join(text.split("\n", n)[:n]) def ps(sort=None): cmd = ['ps', '-eo', 'user,pid,pcpu,pmem,stat,start,time,cmd'] if sort is not None: cmd += ["--sort", sort] return subprocess.check_output(cmd, universal_newlines=True) if __name__ == "__main__": print(text_head(ps("-%cpu"), 6)) For long strings and small n text.split(\n", n)[:n] is a bit faster than text.splitlines()[n] $ wc -l fodder.txt 969 fodder.txt $ python3 -m timeit -s 'text = open("fodder.txt").read()' 'text.split("\n", 6)[:6]' 100000 loops, best of 3: 7.54 usec per loop $ python3 -m timeit -s 'text = open("fodder.txt").read()' 'text.splitlines() [:6]' 1000 loops, best of 3: 215 usec per loop but the effect on the total time to run the code should be negligable.