Iakona
Iakona

Reputation: 157

downloading a file, not the contents

I am trying to automate downloading a .Z file from a website, but the file I get is 2kb when it should be around 700 kb and it contains a list of the contents of the page (ie: all the files available for download). I am able to download it manually without a problem. I have tried urllib and urllib2 and different configurations of each, but each does the same thing. I should add that the urlVar and fileName variables are generated in a different part of the code, but I have given an example of each here to demonstrate.

import urllib2
urlVar = "ftp://www.ngs.noaa.gov/cors/rinex/2014/100/txga/txga1000.14d.Z"
fileName = txga1000.14d.Z
downFile = urllib2.urlopen(urlVar)
with open(fileName, "wb") as f:
    f.write(downFile.read())

Upvotes: 1

Views: 124

Answers (2)

kevr
kevr

Reputation: 455

I was able to download what seems like the correct size for your file with the following python2 code:

import urllib2
filename = "txga1000.14d.Z"
url = "ftp://www.ngs.noaa.gov/cors/rinex/2014/100/txga/{}".format(filename)
reply = urllib2.urlopen(url)
buf = reply.read()

with open(filename, "wb") as fh:
    fh.write(buf)

Edit: The post above me was answered faster and is much better.. I thought I'd post since I tested and wrote this out anyways.

Upvotes: 1

DrV
DrV

Reputation: 23500

At least the urllib2documentation suggest you should use the Requestobject. This works with me:

import urllib2

req = urllib2.Request("ftp://www.ngs.noaa.gov/cors/rinex/2014/100/txga/txga1000.14d.Z")
response = urllib2.urlopen(req)
data = response.read()

Data length seems to be 740725.

Upvotes: 2

Related Questions