Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic http file downloading and saving to disk in python?

I'm new to Python and I've been going through the Q&A on this site, for an answer to my question. However, I'm a beginner and I find it difficult to understand some of the solutions. I need a very basic solution.

Could someone please explain a simple solution to 'Downloading a file through http' and 'Saving it to disk, in Windows', to me?

I'm not sure how to use shutil and os modules, either.

The file I want to download is under 500 MB and is an .gz archive file.If someone can explain how to extract the archive and utilise the files in it also, that would be great!

Here's a partial solution, that I wrote from various answers combined:

import requests import os import shutil  global dump  def download_file():     global dump     url = "http://randomsite.com/file.gz"     file = requests.get(url, stream=True)     dump = file.raw  def save_file():     global dump     location = os.path.abspath("D:\folder\file.gz")     with open("file.gz", 'wb') as location:         shutil.copyfileobj(dump, location)     del dump 

Could someone point out errors (beginner level) and explain any easier methods to do this?

Thanks!

like image 872
arvindch Avatar asked Oct 26 '13 04:10

arvindch


2 Answers

A clean way to download a file is:

import urllib  testfile = urllib.URLopener() testfile.retrieve("http://randomsite.com/file.gz", "file.gz") 

This downloads a file from a website and names it file.gz. This is one of my favorite solutions, from Downloading a picture via urllib and python.

This example uses the urllib library, and it will directly retrieve the file form a source.

like image 122
Blue Ice Avatar answered Sep 22 '22 19:09

Blue Ice


As mentioned here:

import urllib urllib.urlretrieve ("http://randomsite.com/file.gz", "file.gz") 

EDIT: If you still want to use requests, take a look at this question or this one.

like image 27
dparpyani Avatar answered Sep 26 '22 19:09

dparpyani