After installing pyexcel-xlsxr, running the following file gives an unhelpful error message:
from pyexcel_xlsxr import get_data
get_data('/')
UnboundLocalError: local variable 'reader' referenced before assignment
Looking at the source code for io.py, it's clear the error stems from here:
try:
reader = READERS.get_a_plugin(file_type, library)
except NoSupportingPluginFound:
if file_name:
if not os.path.exists(file_name):
raise IOError("%s does not exist" % file_name)
else:
raise
The except block is entered, but the first if is true while the second if is false (because the path does exist, it is just a directory rather than a file), so no new exception is raised. The code continues on until reader is referenced again, which fails because it was never defined.
Expected behavior:
That os.path.exists check should probably be os.path.isfile instead, and the error message should be "%s is not a file" rather than "%s does not exist". That if should also then have an else that raises an exception, so you are never stuck in a position where reader is undefined and you are left with an UnboundLocalError. Something like this:
try:
reader = READERS.get_a_plugin(file_type, library)
except NoSupportingPluginFound:
if not os.path.isfile(file_name):
raise IOError("%s is not a file" % file_name)
else:
raise
After installing
pyexcel-xlsxr
, running the following file gives an unhelpful error message:Looking at the source code for
io.py
, it's clear the error stems from here:The
except
block is entered, but the firstif
is true while the secondif
is false (because the path does exist, it is just a directory rather than a file), so no new exception is raised. The code continues on untilreader
is referenced again, which fails because it was never defined.Expected behavior:
That
os.path.exists
check should probably beos.path.isfile
instead, and the error message should be"%s is not a file"
rather than"%s does not exist"
. Thatif
should also then have anelse
that raises an exception, so you are never stuck in a position wherereader
is undefined and you are left with anUnboundLocalError
. Something like this: