Wednesday, 15 July 2009

Reversing Lines In A File

As is often the case, an interesting little text manipulation task came up in the office today; given a file containing lines of text, reverse it (i.e. put the last line first, the penultimate line second, etc.)

I have a feeling there's a neat little command-line utility that already does this (but can't remember what it is...), but my mind soon went to a short Python script. Here's my first stab at this:

#!/usr/bin/env python
import sys

lines = []
for line in open(sys.argv[1]):
lines.append(line.strip())

lines.reverse()
for line in lines:
print line


After this, I tried to shorten it a bit. Putting the lines into a list could be done nicely with a list comprehension,

#!/usr/bin/env python
import sys

lines = [line.strip() for line in open(sys.argv[1])]

lines.reverse()
for line in lines:
print line


Finally, I discovered the reversed() function, which allows you to create reverse iterators for any Python sequence. Here's where the list comprehension and Python's iteration stuff really comes into its own, leading to a wonderful two-liner (not counting the module import and the #!)


#!/usr/bin/env python
import sys
for line in reversed([line.strip() for line in open(sys.argv[1])]):
print line