From 8cfc654c1791fd85ef4933c39f54059dd3c82813 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 8 Nov 2015 09:44:08 +0100 Subject: [PATCH] filehandle should always be defined --- patacrep/encoding.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/patacrep/encoding.py b/patacrep/encoding.py index 3f1512cf..8ce9c449 100644 --- a/patacrep/encoding.py +++ b/patacrep/encoding.py @@ -29,6 +29,7 @@ def detect_encoding(filename): """Return the most likely encoding of the file """ encodings = ['utf-8', 'windows-1250', 'windows-1252'] + filehandler = None for encoding in encodings: try: filehandler = codecs.open(filename, 'r', encoding=encoding) @@ -41,5 +42,6 @@ def detect_encoding(filename): LOGGER.info('Opening `{}` with `{}` encoding'.format(filename, encoding)) return encoding finally: - filehandler.close() + if filehandler: + filehandler.close() raise UnicodeError('Not suitable encoding found for {}'.format(filename))