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
Actually the simplest way is :
import urllib2 # the lib that handles the url stuff data = urllib2.urlopen(target_url) # it's a file like object and works just like a file for line in data: # 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
Edit 09/2016: In python 3 and up use urllib.request instead of urllib2
I'm a newbie to Python and the offhand comment about Python 3 in the accepted solution was confusing. For posterity, the code to do this in Python 3 is
import urllib.request data = urllib.request.urlopen(target_url) for line in data: ...
from urllib.request import urlopen data = urlopen(target_url)
Note that just
import urllib does not work.