From ef40b94a87fc36b5c8b3446a94330a5b877146e1 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sat, 20 Jun 2015 23:03:53 +0200 Subject: [PATCH] use with open --- homeassistant/components/notify/file.py | 28 ++++++++++++------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/notify/file.py b/homeassistant/components/notify/file.py index 84a93beee58..186bc53ca98 100644 --- a/homeassistant/components/notify/file.py +++ b/homeassistant/components/notify/file.py @@ -62,19 +62,17 @@ class FileNotificationService(BaseNotificationService): def send_message(self, message="", **kwargs): """ Send a message to a file. """ - file = open(self.filepath, 'a') - if os.stat(self.filepath).st_size == 0: - title = '{} notifications (Log started: {})\n{}\n'.format( - kwargs.get(ATTR_TITLE), - dt_util.strip_microseconds(dt_util.utcnow()), - '-'*80) - file.write(title) + with open(self.filepath, 'a') as file: + if os.stat(self.filepath).st_size == 0: + title = '{} notifications (Log started: {})\n{}\n'.format( + kwargs.get(ATTR_TITLE), + dt_util.strip_microseconds(dt_util.utcnow()), + '-'*80) + file.write(title) - if self.add_timestamp == 1: - text = '{} {}\n'.format(dt_util.utcnow(), message) - file.write(text) - else: - text = '{}\n'.format(message) - file.write(text) - - file.close() + if self.add_timestamp == 1: + text = '{} {}\n'.format(dt_util.utcnow(), message) + file.write(text) + else: + text = '{}\n'.format(message) + file.write(text)