while c = f.read(1)

Paul Rubin http
Fri Aug 19 12:43:31 CEST 2005


<en.karpachov at ospaz.ru> writes:
> import itertools
> f = open("blah.txt", "r")
> for c in itertools.chain(*f):
>      print c
>      # ...
> 
> The "f" is iterable itself, yielding a new line from the file every time.
> Lines are iterable as well, so the itertools.chain iterates through each
> line and yields a character.

But that can burn an unlimited amount of memory if there are long
stretches of the file with no newlines.  There's no real good way
around ugly code.



More information about the Python-list mailing list