Download the "head" of a large file?
John Yeung
gallium.arsenide at gmail.com
Mon Jul 27 18:40:25 EDT 2009
On Jul 27, 4:38 pm, erikcw <erikwickst... at gmail.com> wrote:
> I'm trying to figure out how to download just the first few lines of a
> large (50mb) text file form a server to save bandwidth. Can Python do
> this?
>
> Something like the Python equivalent of curlhttp://url.com/file.xml|
> head -c 2048
urllib.urlopen gives you a file-like object, which you can then read
line by line or in fixed-size chunks. For example:
import urllib
chunk = urllib.urlopen('http://url.com/file.xml').read(2048)
At that point, chunk is just bytes, which you can write to a local
file, print, or whatever it is you want.
John
More information about the Python-list
mailing list