pythonpython-2.7downloadurlliburlretrieve

How to Download only the first x bytes of data Python


Situation: The file to be downloaded is a large file (>100MB). It takes quite some time, especially with slow internet connection.

Problem: However, I just need the file header (the first 512 bytes), which will decide if the whole file needs to be downloaded or not.

Question: Is there a way to do download only the first 512 bytes of a file?

Additional information: Currently the download is done using urllib.urlretrieve in Python2.7


Solution

  • I think curl and head would work better than a Python solution here:

    curl https://my.website.com/file.txt | head -c 512 > header.txt

    EDIT: Also, if you absolutely must have it in a Python script, you can use subprocess to perform the curl piped to head command execution

    EDIT 2: For a fully Python solution: The urlopen function (urllib2.urlopen in Python 2, and urllib.request.urlopen in Python 3) returns a file-like stream that you can use the read function on, which allows you to specify a number of bytes. For example, urllib2.urlopen(my_url).read(512) will return the first 512 bytes of my_url