itdraftsman
itdraftsman

Reputation: 3

Pull name for file from array when downloading using urllib

I am trying to use the urllib library for python to pull various tools from urls stored in an array. When I go to the option to save them to a location, I would like to name each file after an entry in a separate array. Every time I try and run my code as written here:

import urllib

url = ["http://dw.cbsi.com/redir?ttag=restart_download_click&ptid=3001&pagetype=product_pdl&astid=2&edid=3&tag=link&siteid=4&destUrl=&onid=2239&oid=3001-2239_4-10320142&rsid=cbsidownloadcomsite&sl=en&sc=us&topicguid=security%2Fantivirus&topicbrcrm=&pid=14314872&mfgid=10044820&merid=10044820&ctype=dm&cval=NONE&devicetype=desktop&pguid=4ca3cb3823b670cd4386478c&viewguid=V4afV1BQaWxD1Ku2AKu@IeHzq6uWztjV6P2F&destUrl=http%3A%2F%2Fsoftware-files-a.cnet.com%2Fs%2Fsoftware%2F14%2F31%2F48%2F72%2Favg_free_stb_all_5961p1_177.exe%3Ftoken%3D1433990253_8690d8cb94b227464de5d6e1d59d78b4%26fileName%3Davg_free_stb_all_5961p1_177.exe","http://download.piriform.com/ccsetup506.exe"]
nameList = ["avg.exe","ccleaner.exe"]
for x in url and nameList:
    urllib.urlretrieve(x, "C:\\Users\\myName\\Desktop\\"+ nameList[x])

I receive the error

Traceback (most recent call last):
  File "P:\PythonProjects\programDownloader.py", line 6, in <module>
    urllib.urlretrieve(x, "C:\\Users\\myName\\Desktop\\"+ nameList[x])
TypeError: list indices must be integers, not str

Can anyone help me with this?

Upvotes: 0

Views: 225

Answers (2)

Iron Fist
Iron Fist

Reputation: 10951

Your mistake is that you are indexing the list with a string, instead of integer, as you stated in your for loop:

for x in url and nameList: #x is string here.

So you need to index your nameList with an integer instead, like follows:

for i, x in enumerate(url):
    urllib.urlretrieve(x, "C:\\Users\\myName\\Desktop\\"+ nameList[i])

Upvotes: 1

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

zip the lists together if you want to match up corresponding elements from each list an unpack in the for loop:

url = ["http://dw.cbsi.com/redir?ttag=restart_download_click&ptid=3001&pagetype=product_pdl&astid=2&edid=3&tag=link&siteid=4&destUrl=&onid=2239&oid=3001-2239_4-10320142&rsid=cbsidownloadcomsite&sl=en&sc=us&topicguid=security%2Fantivirus&topicbrcrm=&pid=14314872&mfgid=10044820&merid=10044820&ctype=dm&cval=NONE&devicetype=desktop&pguid=4ca3cb3823b670cd4386478c&viewguid=V4afV1BQaWxD1Ku2AKu@IeHzq6uWztjV6P2F&destUrl=http%3A%2F%2Fsoftware-files-a.cnet.com%2Fs%2Fsoftware%2F14%2F31%2F48%2F72%2Favg_free_stb_all_5961p1_177.exe%3Ftoken%3D1433990253_8690d8cb94b227464de5d6e1d59d78b4%26fileName%3Davg_free_stb_all_5961p1_177.exe","http://download.piriform.com/ccsetup506.exe"]
nameList = ["avg.exe","ccleaner.exe"]
for x, name in zip(url,nameList):
    urllib.urlretrieve(x, "C:\\Users\\myName\\Desktop\\"+ name)

You are trying to index nameList with the string x which each string from Namelist i.e nameList["avg.exe"], that is why your code errors.

If you wanted to index you would need enumerate:

for ind, x in enumerate(url):
    urllib.urlretrieve(x, "C:\\Users\\myName\\Desktop\\"+ nameList[ind])

where ind is the index of each element in the url list.

Upvotes: 2

Related Questions