2
$\begingroup$

I came across this statement from CNTK 103A:

gzfname, h = urlretrieve(src, './delete.me')

I understand all the rest but that gzfname, h = ......

What exactly is the purpose of h, is it a filehead thing?

Code:

def load_or_download_mnist_files(filename, num_samples, local_data_dir):
    gzfname, h = urlretrieve(src, './delete.me')
    if (local_data_dir):
        local_path = os.path.join(local_data_dir, filename)
    else:
        local_path = os.path.join(os.getcwd(), filename)

    if os.path.exists(local_path):
        gzfname = local_path
    else:
        local_data_dir = os.path.dirname(local_path)
        if not os.path.exists(local_data_dir):
            os.makedirs(local_data_dir)
        filename = "http://yann.lecun.com/exdb/mnist/" + filename
        print ("Downloading from" + filename, end=" ")
        gzfname, h = urlretrieve(filename, local_path)
        print ("[Done]")

    return gzfname
$\endgroup$

1 Answer 1

1
$\begingroup$

gzfname, h is doing tuple unpacking from the return of urlretrieve. From the docs, urlretrieve returns:

Return a tuple (filename, headers) where filename is the local file name under which the object can be found, and headers is whatever the info() method of the object returned by urlopen() returned (for a remote object). Exceptions are the same as for urlopen().

And the info() method of the urlopen object returns:

info() — return the meta-information of the page, such as headers, in the form of an email.message_from_string() instance (see Quick Reference to HTTP Headers)

So, h is meta information about the retrieved page.

$\endgroup$
2
  • $\begingroup$ so if h is of no use (and could not find any subsequent use in the code), then I can also do without it by using gz = urlretrieve.....? $\endgroup$
    – r poon
    Commented Jan 3, 2018 at 7:31
  • $\begingroup$ You can not ignore the h completely. You must either use gzfname = urlretrieve(...)[0] to select the first element of the tuple, or gzfname, _ = urlretrieve(...) can be used by convention to set the value into _ which is the convention for assigning to a name you want to ignore. $\endgroup$
    – Stephen Rauch
    Commented Jan 3, 2018 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.