0

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)
0

1 Answer 1

2

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.

1
  • Yes. This is exactly what I wanted to hear. Thank you so much. I searched a lot but there were no answer for this.
    – Ramin-RX7
    Commented Jul 1, 2020 at 16:46

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