0

How do I download a file in python to local directory C:\1\1. I see lot of examples but most seem to be 5+ years old with out of date information. Thanks.

 import urllib.request

url = "http://download.thinkbroadband.com/10MB.zip"

file_name = url.split('/')[-1]
u = urllib.request.urlretrieve.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print("Downloading: %s Bytes: %s" % (file_name, file_size))

file_size_dl = 0
block_sz = 8192
while True:
    buffer = u.read(block_sz)
    if not buffer:
        break

    file_size_dl += len(buffer)
    f.write(buffer)
    status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
    status = status + chr(8)*(len(status)+1)
    print(status,)

f.close()

Error: function has no attribute url open

AttributeError: 'function' object has no attribute 'urlopen'

Can someone explain to me how i download a simple zip from a website this does not work for me. Cheers.

  • Go to this link https://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python – Jai Prak Jan 12 '18 at 09:24
  • Possible duplicate of [How do I download a file over HTTP using Python?](https://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python) – Tom Dalton Jan 12 '18 at 09:25
  • @TomDalton Thanks, that's the 5+ year old solution I mentioned in my post. –  Jan 12 '18 at 09:26
  • Yep - in future you should use the 'close' vote above, and choose the 'duplicate' reason. – Tom Dalton Jan 12 '18 at 09:27
  • I also get HTTP ERROR 404: Not found despite correct link. These answer in that question are outdated and only workable if you live in 2008 –  Jan 12 '18 at 09:40

1 Answers1

0
import requests

url = "http://download.thinkbroadband.com/10MB.zip"
src = r"C:\1\1\downloaded_zip.zip"


respo = requests.get(url, stream=True, verify=False)
if respo.status_code == requests.codes.ok:
    out = open(src, "wb")
    for block in respo.iter_content(1024):
        if not block:
            break

        out.write(block)

    out.close()
else:
    print("Not able to download ZIP url {url}: {status}, {content}".format(url=url, status=respo.status_code, content=respo.content))
Lupanoide
  • 3,132
  • 20
  • 36
  • Is this method okay for downloading large 60mb files on a regular basis out of curiosity? Still thanks, I spent a while going through an endless loop of deprecated answers. –  Jan 12 '18 at 09:56
  • yep, with this method you are able to download a file by chuncks, you will not have trouble also with large files. – Lupanoide Jan 12 '18 at 09:58
  • Glad to hear it. What about authentication where I have some zipped music files on sites like bitbucket and sabertooth. respo = requests.get(url, stream=True, verify=False, auth=('Username', 'password')).-> 401, b'' –  Jan 12 '18 at 10:19
  • @imapotatofish take a look here: http://docs.python-requests.org/en/master/user/authentication/ – Lupanoide Jan 12 '18 at 10:20
  • Uh right. Well I tried both those examples and same error. Perhaps not supported on bitbucket not sure about sabertooth –  Jan 12 '18 at 10:26
  • you should remember that google is your friend: https://bitbucket.org/atlassian/python-bitbucket https://github.com/Sheeprider/BitBucket-api – Lupanoide Jan 12 '18 at 10:36
  • let check the usage of requests library on bitbucket here: https://github.com/Sheeprider/BitBucket-api/blob/master/bitbucket/bitbucket.py – Lupanoide Jan 12 '18 at 10:38
  • https://developer.atlassian.com/blog/2016/02/bitbucket-oauth-with-python/ – Lupanoide Jan 12 '18 at 11:00
  • Cheers. I see that sheeprider is quite old with no maintainer, python-bitbucket has no docs and no simple auth works on blog post. 401 b''. I'll see if Oauth2 works tho, here's hoping as other methods look confusing –  Jan 12 '18 at 11:14
  • if you don't like that solution you could check for other bitbucket api in python on github. there are more and more than those ones that i have showed to you. https://github.com/search?l=Python&q=bitbucket&type=Repositories&utf8=%E2%9C%93 Google is your friend – Lupanoide Jan 12 '18 at 11:20
  • I am definitely stuck. I posted a new question on this. –  Jan 12 '18 at 11:56