Use urlopen. Since urlretrieve generates a file at the start of download, incomplete files will remain if the download fails or is interrupted.
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).
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