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
Post a Comment