Python equivalent of a given wget command

前端 未结 10 2225
面向向阳花
面向向阳花 2020-12-02 10:04

I\'m trying to create a Python function that does the same thing as this wget command:

wget -c --read-timeout=5 --tries=0 \"$URL\"

-c

10条回答
  •  醉酒成梦
    2020-12-02 10:43

    Here's the code adopted from the torchvision library:

    import urllib
    
    def download_url(url, root, filename=None):
        """Download a file from a url and place it in root.
        Args:
            url (str): URL to download file from
            root (str): Directory to place downloaded file in
            filename (str, optional): Name to save the file under. If None, use the basename of the URL
        """
    
        root = os.path.expanduser(root)
        if not filename:
            filename = os.path.basename(url)
        fpath = os.path.join(root, filename)
    
        os.makedirs(root, exist_ok=True)
    
        try:
            print('Downloading ' + url + ' to ' + fpath)
            urllib.request.urlretrieve(url, fpath)
        except (urllib.error.URLError, IOError) as e:
            if url[:5] == 'https':
                url = url.replace('https:', 'http:')
                print('Failed download. Trying https -> http instead.'
                        ' Downloading ' + url + ' to ' + fpath)
                urllib.request.urlretrieve(url, fpath)
    

    If you are ok to take dependency on torchvision library then you also also simply do:

    from torchvision.datasets.utils import download_url
    download_url('http://something.com/file.zip', '~/my_folder`)
    

提交回复
热议问题