Why you should use urlopen instead of urlretrieve to download Python files

Conclusion

Use urlopen. Since urlretrieve generates a file at the start of download, incomplete files will remain if the download fails or is interrupted.

Overview

When downloading a file in Python, urlretrieve is the simplest to write.

urlretrieve


import urllib

urllib.request.urlretrieve(download_url, save_dir)

urlretrieve seems to be deprecated. If you do the same thing, urlopen seems to be recommended. However, urlopen is a little as follows, but the description is complicated, so if there is no problem, I would like to use url retrieve.

urlopen


import urllib
data = urllib.request.urlopen(download_url).read()
with open(save_dir, mode="wb") as f:
    f.write(data)

urlopen VS urlretrieve

urlopen has the advantage of having the timeout option, but that's trivial. There are major reasons why you shouldn't use urlretrieve </ b>. It is as follows.

--urlretireve creates a file at the beginning of the download. --urlopen creates a file after the download is complete.

urlretireve will generate the file first regardless of the success or failure of the download. It is an image of writing data to the created file at any time. In other words, if the download fails or is interrupted for some reason, an incomplete file will be created (it will be a corrupted file and cannot be read at all).

At the end

I was a little addicted to it, but I didn't have any information, so I wrote an article. I think it's a fatal problem, so don't use uriretrieve.

Recommended Posts

Why you should use urlopen instead of urlretrieve to download Python files
Why you should use Pandas apply ()
[Python] Summary of how to use pandas
[Python2.7] Summary of how to use unittest
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
Use boto to upload / download files to s3.
[Question] How to use plot_surface of python
[Python] How to use two types of type ()
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
Batch conversion of Excel files to JSON [Python]
Summary of studying Python to use AWS Lambda
You should know if you use Python! 10 useful libraries
I tried to summarize how to use matplotlib of python
Use urlparse.urljoin instead of os.path.join for Python URL joins
How to use Python Kivy ① ~ Basics of Kv Language ~
How to download files from Selenium in Python in Chrome
python> shutil.copytree ()> do not overwrite files> use distutils.dir_util.copy_tree () instead
Site notes to help you use NetworkX with Python
Three Reasons Why Machine Learning Learners Should Use Python
Solution when you want to use cv_bridge with python3 (virtualenv)
[Python] Summary of how to use split and join functions
[Python] Use this to read and write wav files [wavio]
[Free to use] 7 learning sites where you can study Python
When you want to use multiple versions of the same Python library (virtual environment using venv)
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
A note on what you did to use Flycheck with Python
You should not use the --color = always option of the grep command
How to use functions in separate files Perl and Python versions
Sample to use after OAuth authentication of BOX API with Python
How to use Python Kivy (reference) -I translated Kivy Language of API reference-
Basic grammar of Python3 system (how to use functions, closures, lambda functions)
[Python] When you want to use all variables in another file
Indispensable if you use Python! How to use Numpy to speed up operations!
It is convenient to use Icecream instead of print when debugging.