Chris Chris - 11 months ago 37
Python Question

In Python, given a URL to a text file, what is the simplest way to read the contents of the text file?

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?

#read the file
#print first line
#print second line

Answer Source

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("").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