Robin Kohrs
Robin Kohrs

Reputation: 697

Use urlretrieve to download file from url if not yet existing

I'm trying to write some lines to download a file from a website to a local path on the computer, that the user provides by ds_dir . If the path already exists I would like to give back an error message. My current code looks like this and is part of a function:


        #respond to users choice
        if choice == "y" or "Y":
            if not os.Path.exists(ds_dir):
                try:
                    urllib.request.urlretrieve(url, ds_dir)
                    print("Downloading...")
                    return ("you just downloaded the file to: {ds_dir}".format(ds_dir))

                except Exception:
                    log.exception(f"ERROR DURING DOWNLOAD: {ds_dir} FROM {url}.")

Now it appears that it's not working at all. The ds_dir is the directory the user want to save it to. Anybody a idea of how to fix this? Could be a whole different approach as well.

Upvotes: 0

Views: 985

Answers (1)

Andrea Grioni
Andrea Grioni

Reputation: 187

you if statement is correct, but you misspelled 'path':

it should be:

os.path.exists(ds_dir)

Upvotes: 1

Related Questions