new linereading standard?

Neel Krishnaswami neelk at brick.cswv.com
Tue Apr 25 18:14:34 EDT 2000


Pete Shinners <pete at visionart.com> wrote:
> 
> i'm sure someone has posted a class like the before, but i'm hoping it
> can one day make it in as a python standard someday. (6.0, there's still
> time!)
> 
> class filelines():
>     def __init__(self, file):
>         self.file = file
>     def __getitem__(self, index):
>         line = self.file.readline()
>         if not line: raise IndexError
>         return line
> 
> anyways, i now use this code ALWAYS. unfortunately, i haven't found a
> good way to make this part of my standard environment while working on
> many different machines.

I use something like this whenever the file is too big for
file.readlines(), too.

A couple of notes:

o If you need this to be part of your standard environment, you should
  (as others have mentioned) look at the 'fileinput' standard module.

o If the wrapper approach is too slow, then you might want to look at 
  the QIO module at

  http://members.xoom.com/meowing/python/

  This is -blazingly- fast; it lets you step through files about 2-3
  times faster than using standard file objects, even without wrappers,
  and lets you use friendly syntax. This is especially helpful if you 
  have great big files (that are too big for file.readlines()). 


Neel



More information about the Python-list mailing list