'Downloading file in python (using urllib and shutil) [duplicate]

I was searching for python script to download files and I found 2 ways of downloading with urllib.request.urlopen. One of them use shutil module and the other one not.

Now I want to know what is the difference between these 2 script?

# 1:
import urllib
filedata = urllib.request.urlopen('http://download.thinkbroadband.com/10MB.zip')
datatowrite = filedata.read()
with open('10MB.zip', 'wb') as f:
    f.write(datatowrite)

# 2:
import urllib,shutil
with urllib.request.urlopen('http://download.thinkbroadband.com/10MB.zip') as response, open("10MB.zip", 'wb') as f:
    shutil.copyfileobj(response, f)


Solution 1:[1]

Both are fine and the difference is subtle.

According to the urllib docs, urlopen always returns an object which can work as a context manager (ie. can be used in a with-statement).

In example #1, all data is read from the response and stored in memory (variable datatowrite). Afterwards it is written to a file, from this variable.

In example #2, the data is directly read in chunks from the response to the file (by shutil.copyfileobj).

Imho, example #2 uses less memory, especially when downloading big files.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeroen Vankelecom