stendarr stendarr -3 years ago 296
Python Question

Download file with urlretrieve() to subfolder

Is it possible to use

urlretrieve()
in order to download something into a subfolder without expressing it in an absolute but relative manner?
For example:

urllib.request.urlretrieve(url, '/downloads/2017/foo.txt')


Everytime I add a path to the filename python throws following error:


File "C:\Program Files\Python36\lib\urllib\request.py", line 258, in urlretrieve
tfp = open(filename, 'wb')
FileNotFoundError: [Errno 2] No such file or directory: '/downloads/2017/foo.txt'


But when I use this code:

urllib.request.urlretrieve(url, 'foo.txt')


it happily downloads the file.

I think I am missing something basic here, but after searching the interwebs for quite a while I haven't found an answer yet. Does anyone know how relative filepaths should be expressed in the
urlretrieve()
method?

Answer Source

When using urllib.request.urlretrieve(), you need to make sure that, if you want to save the file somewhere else than the program's location, the folders you are referencing already exist. Alternatively you can use the following code to generate the folders you need if they do not exist:

for i in directories:
    if not os.path.isdir(i):
        os.makedirs(i)

Check for spelling mistakes if you still get a FileNotFoundError.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download