Download the "head" of a large file?

Ben Charrow bcharrow at csail.mit.edu
Mon Jul 27 18:33:51 EDT 2009


erikcw wrote:
> ...download just the first few lines of a large (50mb) text file form a
> server to save bandwidth..... Something like the Python equivalent of curl
> http://url.com/file.xml | head -c 2048

If you're OK calling curl and head from within python:

from subprocess import Popen, PIPE
url = "http://docs.python.org/"
p1 = Popen(["curl", url], stdout = PIPE, stderr = PIPE)
p2 = Popen(["head", "-c", "1024"], stdin = p1.stdout, stdout = PIPE)
p2.communicate()[0]

If you want a pure python approach:

import urllib2
url = "http://docs.python.org/"
req = urllib2.Request(url)
f = urllib2.urlopen(req)
f.read(1024)

HTH,
Ben



More information about the Python-list mailing list