On Thu, Oct 1, 2015 at 3:58 PM, Cecil Westerhof <ce...@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. -- https://mail.python.org/mailman/listinfo/python-list