reading specific lines of a file

Bill Pursell bill.pursell at gmail.com
Sat Jul 15 17:12:19 EDT 2006


Yi Xing wrote:
> I want to read specific lines of a huge txt file (I know the line #).
> Each line might have different sizes. Is there a convenient and fast
> way of doing this in Python? Thanks.

#!/usr/bin/env python

import os,sys
line = int(sys.argv[1])
path = sys.argv[2]
os.system("sed -n %dp %s"%(line,path))


Some might argue that this is not really doing
it in Python.  In fact, I would argue that!  But if
you're at a command prompt and you want to
see line 7358, it's much easier to type
% sed -n 7358p
than it is to write the python one-liner.




More information about the Python-list mailing list