Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97316

Re: Only getting the first 6 lines

Path csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.042
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'lines.': 0.07; 'subject:getting': 0.07; 'output,': 0.09; 'skip:= 70': 0.10; "skip:' 30": 0.15; 'thu,': 0.15; 'stdin=none,': 0.16; 'subprocess': 0.16; 'wrote:': 0.16; '2015': 0.20; "skip:' 40": 0.22; 'skip:( 40': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'this?': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'lines': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'does': 0.39; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'forget': 0.60; 'entire': 0.61; 'six': 0.65; 'cecil': 0.84; 'to:name:python': 0.84; 'westerhof': 0.84; 'subject:Only': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=BN/XcWidqE3SEovKPbSrFsM59dvsJGTODE1fFFq7HXw=; b=VBc1aivvqEfzzo+DZ26bge+et2+5yd8ejlKpwJmqmKfk8FRAr9CbiNcr3ogWZMJ9er h86xB5MVdhCsLzErCstpaTvqHW4Di9oa74rKG5twKUUDYOxOJV+bDi/bwAPJkMJpkdOc 8li9B5kllTfH7auf9X5J9991CHsF84tFaMD3Gh9QXpveStM0RzshQODymhhaMwkGXZon plHUifx9JkwrwGGKZY9wm6jLUNiD+W9FeL42I57estMhMd0RChwsDIk9PAd2oHkpJ87f +DSVvh76ClENrQtZoaz1an1554QmdVI8dNtUb2i2YImxZYeG6MRdlRHzLRjJQGFY/qGo 60mw==
X-Received by 10.129.103.67 with SMTP id b64mr10635278ywc.55.1443740321830; Thu, 01 Oct 2015 15:58:41 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <87y4fmp7xi.fsf@Equus.decebal.nl>
References <87y4fmp7xi.fsf@Equus.decebal.nl>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 1 Oct 2015 16:58:02 -0600
Subject Re: Only getting the first 6 lines
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.320.1443740330.28679.python-list@python.org> (permalink)
Lines 16
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1443740330 news.xs4all.nl 23854 [2001:888:2000:d::a6]:53364
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97316

Show key headers only | View raw


On Thu, Oct 1, 2015 at 3:58 PM, Cecil Westerhof <Cecil@decebal.nl> 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?

Instead of using check_output which reads the entire output, you could
create a Popen with stdin=None, stdout=PIPE, stderr=None, and then
read just the first six lines. Don't forget to clean up the subprocess
afterward.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Only getting the first 6 lines Cecil Westerhof <Cecil@decebal.nl> - 2015-10-01 23:58 +0200
  Re: Only getting the first 6 lines Ian Kelly <ian.g.kelly@gmail.com> - 2015-10-01 16:58 -0600
  Re: Only getting the first 6 lines Cameron Simpson <cs@zip.com.au> - 2015-10-02 08:50 +1000
    Re: Only getting the first 6 lines Cecil Westerhof <Cecil@decebal.nl> - 2015-10-02 13:08 +0200
  Re: Only getting the first 6 lines Peter Otten <__peter__@web.de> - 2015-10-02 09:37 +0200
    Re: Only getting the first 6 lines Cecil Westerhof <Cecil@decebal.nl> - 2015-10-02 13:11 +0200

csiph-web