[Baypiggies] Using urllib2 to print file contents line-by-line

ken barclay ken.barclay at att.net
Sat Dec 19 04:39:30 CET 2009


Hello,

I'm trying to do something simple - I want to print the contents of files that are staged on a web server back to the browser. I loop over the file-like object that comes back in the response:

        import urllib2
        req = urllib2.Request(href)
        response = urllib2.urlopen(req)
        for line in response:
        print line

But I want the lines to appear in the browser exactly like they do in the file - with each one on a new line (because it's columnar data.) The problem is: this approach doesn't work because the first 'print' is printing out the whole response in one big stream (ignoring newlines).

I tried the other techniques at http://docs.python.org/library/urllib2.html still get the same result. Suggestions?

Thanks
Ken
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20091218/a54bcdf1/attachment.htm>


More information about the Baypiggies mailing list