tags:

views:

214

answers:

4

In Python, when given the URL for a text file, what is the simplest way to access the contents off the text file and print the contents of the file out locally line-by-line without saving a local copy of the text file?

TargetURL=http://www.myhost.com/SomeFile.txt
#read the file
#print first line
#print second line
#etc
+3  A: 
import urllib2

f = urllib2.urlopen(target_url)
for l in f.readlines():
    print l
Will
+1, but please note that it's the simplest way, NOT THE SAFEST. If any error occurs on the server side and this one delivery content for ever, you could ends up with an infinite loop.
e-satis
+6  A: 
import urllib2
for line in urllib2.urlopen("http://www.myhost.com/SomeFile.txt"):
    print line
Fabian Neumann
+9  A: 

Actually the simplest way is :

import urllib2  # the lib that handles the url stuff

file = urllib2.urlopen(target_url) # it's a file like object and works just like a file
for line in file: # files are iterable
    print line

You don't even need "readlines", as Will suggested. You could even shorten it to

import urllib2

for line in urllib2.urlopen(target_url):
    print line

But remember in Python, readability matters.

However, this is the simplest way but not the safe way because most of the time with network programming, you don't know if the amount of data to expect will be respected. So you'd generally better read a fixed and reasonable amount of data, something you know to be enough for the data you expect but will prevent your script from been flooded :

import urllib2

data = urllib2.urlopen("http://www.google.com").read(20000) # read only 20 000 chars
data = data.split("\n") # then split it into lines

for line in data:
    print line
e-satis
Need a colon on the end of the second example's for line.
Douglas Leeder
Fixed.Thank you.
e-satis
+1  A: 

There's really no need to read line-by-line. You can get the whole thing like this:

import urllib
txt = urllib.urlopen(target_url).read()
Ken