Using xreadlines

Nick Craig-Wood nick at craig-wood.com
Sat Feb 28 05:30:04 EST 2009


bearophileHUGS at lycos.com <bearophileHUGS at lycos.com> wrote:
>  Brett Hedges:
> > My question is how do I go to a previous line in the file? xreadlines has a file.next() statement that gives the next line, and I need a statement that gives me the previous line.<
> 
>  In modern versions of Python you usually don't need xreadlines,
>  because files are iterable.
> 
>  If your files are small, you can just read all the lines in a list
>  with open(...).readlines(), and then just use the item of the list
>  with the n-1 index.

There is also the linecache module

  http://docs.python.org/library/linecache.html

Which may be useful...

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list