[Python-ideas] shutil.runret and shutil.runout
Stephen J. Turnbull
stephen at xemacs.org
Sun Feb 26 15:02:42 CET 2012
Serhiy Storchaka writes:
> Yes, I want this in Python:
> readall(cmd('cut -d: -f3 $file', file='/etc/passwd') | cmd('sort -n') | cmd('tail -n5'))
> cmd('cut', '-d:', '-f3', '/etc/passwd').pipe('sort', '-n').pipe('tail', '-n5').readlines()
> or something similar.
But you can already do
sorted([l.split(":") for l in open('/etc/passwd')])[-5:]
(and I don't really care whether you were being ironic or not; either
way that one-liner is an answer<wink/>).
Actually, I wrote that off the top of my head and it almost worked.
The problem I ran into is that I'm on a Mac, and there was a bunch of
cruft comments (which don't contain any colons) in the beginning of
the file. So I got a list index out of range when accessing the split
line. In this case, cut | sort | tail would produce the expected
output. But cut | sort | head would just produce garbage (the leading
comments in sorted order). So the failure modes differ. It might be
useful for people used to shell failure modes.
More information about the Python-ideas