Overwrite existing files with Python's "wget"?

Viewed 4146

I have installed wget on my Python, and I'm downloading files from different URLs with it. So far my code looks like this:

import wget
urls = ['https://www.iedb.org/downloader.php?file_name=doc/epitope_full_v3.zip', 
        'https://www.iedb.org/downloader.php?file_name=doc/tcell_full_v3.zip',
        'https://www.iedb.org/downloader.php?file_name=doc/bcell_full_v3.zip', 
        'https://www.iedb.org/downloader.php?file_name=doc/mhc_ligand_full_single_file.zip']
path = '/home/david/data/files/zip_files'

for url in urls:
   wget.download(url, path)

I want my code to overwrite the downloaded files if they exist, so that way every time I run the code I get the latest version of that files, instead of keeping the old ones and downloading the new ones with a different name (e.g, if epitope_full_v3.zip already exists, when I execute the code it will download it again, but will keep the old one and rename the new one to epitope_full_v3_1.zip).

I know that wget can take an -O argument in the shell that allows you to do that, but I have not seen that for the python version on the documentation. I appreciate your help.

1 Answers

Although wget doesn't mentioned that, you could change it by yourself.Use os.path.basename() to get the filename, and check whether it exists.Like this:

import wget
import os

urls = ['https://www.iedb.org/downloader.php?file_name=doc/epitope_full_v3.zip',
        'https://www.iedb.org/downloader.php?file_name=doc/tcell_full_v3.zip',
        'https://www.iedb.org/downloader.php?file_name=doc/bcell_full_v3.zip',
        'https://www.iedb.org/downloader.php?file_name=doc/mhc_ligand_full_single_file.zip']

path = '/home/david/data/files/zip_files'

for url in urls:
    filename = path + '/' + os.path.basename(url) # get the full path of the file
    if os.path.exists(filename):
        os.remove(filename) # if exist, remove it directly
    wget.download(url, out=filename) # download it to the specific path.
Related