Browse Source

filehandle should always be defined

pull/168/head
Oliverpool 9 years ago
parent
commit
8cfc654c17
  1. 2
      patacrep/encoding.py

2
patacrep/encoding.py

@ -29,6 +29,7 @@ def detect_encoding(filename):
"""Return the most likely encoding of the file """Return the most likely encoding of the file
""" """
encodings = ['utf-8', 'windows-1250', 'windows-1252'] encodings = ['utf-8', 'windows-1250', 'windows-1252']
filehandler = None
for encoding in encodings: for encoding in encodings:
try: try:
filehandler = codecs.open(filename, 'r', encoding=encoding) filehandler = codecs.open(filename, 'r', encoding=encoding)
@ -41,5 +42,6 @@ def detect_encoding(filename):
LOGGER.info('Opening `{}` with `{}` encoding'.format(filename, encoding)) LOGGER.info('Opening `{}` with `{}` encoding'.format(filename, encoding))
return encoding return encoding
finally: finally:
if filehandler:
filehandler.close() filehandler.close()
raise UnicodeError('Not suitable encoding found for {}'.format(filename)) raise UnicodeError('Not suitable encoding found for {}'.format(filename))

Loading…
Cancel
Save