Python try except block does not recognize error type -


my scrpt has following line:

libro_dia = xlrd.open_workbook(file_contents = libro_dia) 

when libro_dia not valid, raises following error:

xlrderror: unsupported format, or corrupt file: expected bof record; found '<!doctyp' 

i whant handle error, write:

try:     libro_dia = xlrd.open_workbook(file_contents = libro_dia) except xlrderror:     no_termina = false 

but raises following error:

nameerror: name 'xlrderror' not defined 

what's going on?

you don't have xlrderror imported. i'm not familiar xlrd, like:

from xlrd import xlrderror 

might work. alternatively, qualify error when handling it:

try:     libro_dia = xlrd.open_workbook(file_contents = libro_dia) except xlrd.xlrderror: #<-- qualified error here     no_termina = false 

the above assuming have following import:

import xlrd 

in response comment:

there several ways use imports in python. if import using import xlrd, have qualify every object in module xlrd.someobject. alternative way using form from xlrd import *, allow reference xlrd error without its' module namespace. lazy , bad idea though, can lead namespace clashes. if reference error without qualifying it, correct way from xlrd import xlrderror, allow except xlrderror. read more python modules


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -