From 3e8bb3c6a264b0f525a52bcb363e99cb512d750e Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 27 Mar 2015 20:06:18 +0200 Subject: Refactor "f=open()...f.close()" to use "with open() as f:" --- genbackupdata | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'genbackupdata') diff --git a/genbackupdata b/genbackupdata index 7ed1ee8..168961b 100755 --- a/genbackupdata +++ b/genbackupdata @@ -77,13 +77,12 @@ class GenbackupdataApp(cliapp.Application): dirname = os.path.dirname(pathname) if not os.path.exists(dirname): os.makedirs(dirname) - f = open(pathname, 'wb') - while bytes >= chunk_size: - self.write_bytes(f, chunk_size) - bytes -= chunk_size - if bytes > 0: - self.write_bytes(f, bytes) - f.close() + with open(pathname, 'wb') as f: + while bytes >= chunk_size: + self.write_bytes(f, chunk_size) + bytes -= chunk_size + if bytes > 0: + self.write_bytes(f, bytes) def write_bytes(self, f, bytes): chunk = self.gen.generate(bytes) -- cgit v1.2.1