0

I have a routine where I have to read from a Excel which has a column with links to get individual .xlsx/.xls files that people upload into the form containing some information.

My problem is, people do not always upload the correct file format. So I had to create exceptions to handle that. I save the links that have a exception in a list, but I don't know which exception blocked it. Here's my code:

erros = []
for i in links:
    try:
        name = i[50:]
        df = pd.read_excel(i, header = 1, usecols = col_names, encoding = 'utf-8') #usecols = names)
        file_name = r"%s\%s" %(pasta_sol,name)

        writer = pd.ExcelWriter(file_name , engine='xlsxwriter')
        df.to_excel(writer, header = True, index = True)
        writer.close()

    except (TypeError, IndexError, ValueError, XLRDError, BadZipFile, urllib.error.URLError) as e:
        erros.append(i)

There is a way to append to each file that has a exception which one was it? It could be a list or a new df that looks like it:

erros = [['http://abs.company.pdf', 'TypeError'],['http://abs.company.xls','XLRDError']]

or df.

*There are thousands of files to read per day.

Thanks

Gustavo Rottgering
  • 511
  • 1
  • 4
  • 11

1 Answers1

0

This is isnt exactly what you wanted but its close enough. Hope it helps

  errors = []
    for i in links:
        try:
            name = i[50:]
            df = pd.read_excel(i, header = 1, usecols = col_names, encoding = 'utf-8') #usecols = names)
            file_name = r"%s\%s" %(pasta_sol,name)

        writer = pd.ExcelWriter(file_name , engine='xlsxwriter')
        df.to_excel(writer, header = True, index = True)
        writer.close()

    except (TypeError, IndexError, ValueError, XLRDError, BadZipFile, urllib.error.URLError) as e:
        errors.append([file_name, e.args[0]])

print(errors) # doesnt print the error name but the description of the error e.g "division by zero"