Shouldn't urlopen "files" be iterable?

Magnus Lie Hetland mlh at idi.ntnu.no
Sat Sep 29 20:29:51 CEST 2001


"Jarno J Virtanen" <jajvirta at cc.helsinki.fi> wrote in message
news:slrn9raqcm.k93.jajvirta at sirppi.helsinki.fi...
> Sat, 29 Sep 2001 00:33:33 +0200 Magnus Lie Hetland wrote:
> > I just tried to do
> >
> >   for line in urlopen(url): ...
> >
> > but it didn't work... Shouldn't the urllib "files"
> > behave like other files? (Using 2.2a2)
>
> Yeah, you just forgot ".readlines()", e.g.:
>
> for line in urllib.urlopen(url).readlines():
>     ...

No, my good man... I did not forget <wink>. readlines()
would require me to download the whole thing in one
go, and that isn't necessarily what I want.

The point was whether or not they should be iterable,
like normal files. I mean, it is fully possible to
do

  for line in open('foo.txt'):
      bar(line)

(Just check out 2.2 or the iterator PEP.)

This is, however, not possible with the semi-file-like
objects of urllib. I guess this is simply a matter of
lag, but you never know...

--

  Magnus Lie Hetland         http://www.hetland.org

 "Reality is that which, when you stop believing in
  it, doesn't go away."           -- Philip K. Dick






More information about the Python-list mailing list