-3

I am trying to write code that allows me to do 4 things, and I am using try and except.

The code is as follows:

try:
    for i in lista:
        a = url1 + i
        print(a)
        wget.download(a, '/Users/******/downloads')

except:

    for i in lista:
    b = url2 + i
    wget.download(b, '/Users/*****/downloads')

But I need to use 2 more exceptions. Can you explain to me how I can do it?

The main goal is to download a file; if it is still not there, download a second file, and so on and so forth.

  • "I need to use 2 more exceptions"—What 2 exceptions? Why? – khelwood Apr 11 '21 at 15:21
  • Please update the indentation of your code. Python is very sensitive to indentation, as are python programmers. – quamrana Apr 11 '21 at 15:23
  • Hello, thanks for the prompt response, sorry about the indentation, it was a copy paste problem. I wish to download a file, if it is not available download a second one, and if that's not available, download a third one. – Rebeca Campos Apr 11 '21 at 15:23
  • You should be explicit about what `Exception` you expect in the expect block. (you can write as many excepts as you wish but whether you should is the question). That said, I think you have the wrong idea of what except does. Maybe you need `else`? – NelsonGon Apr 11 '21 at 15:25
  • Else won't do the trick I beleive, because else will only run if no exceptions are encountered. – Rebeca Campos Apr 11 '21 at 15:28
  • You should place the for-loop outside the try-except block, then if link 1 (url1) doesn't work, just change to url2 in the except but this will keep you having excepts forever (say you have 100 URLs, then 100+ excepts?). There must be a way to check this only once and avoid several excepts? What's common about these links? – NelsonGon Apr 11 '21 at 15:35
  • Hello Nelson, can you show me how? I only need 4 of them, they are dynamic. I have one for each 15 min, and change 4 times max. – Rebeca Campos Apr 11 '21 at 15:37

1 Answers1

0

You can specify the error after the except statement. For example:

urls = [
    "algumsite.com",
    "outrosite.org",
    "sitezinho.com.br"
]

for url in urls:
    try:
        wget.download(url, "path_to_download_folder/")
    except <error/s that can be raised in the previous try block>:
        # code that will be executed if the error were raised
  • Hello Mauro, almost right, I´m portuguese XD.... Valeu aí... Will try. Thanks a lot – Rebeca Campos Apr 11 '21 at 15:32
  • Poderias por favor dar um exemplo com o meu código? Não estou a entender o que queres dizer com do_stuff(), é uma função? – Rebeca Campos Apr 11 '21 at 15:48
  • @RebecaCampos interpreta isso como um método, ele pode "subir" uma exceção no meio da execução usando `raise `, os blocos except pegam essas exceções e fazem o código fazer outra coisa em vez de parar no meio da execução do programa (essa coisa é definida por ti) –  Apr 11 '21 at 16:21
  • Mais uma vez, não querendo abusar, podes exemplificar com código? o mais parecido com o meu se possível.. – Rebeca Campos Apr 11 '21 at 16:34