From 3e5ef8d7ecf0d0dd7e76ef554564eac9e45019c9 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Fri, 18 Jul 2014 23:10:48 +0000 Subject: [PATCH] filemanip: use with statements to open files. Signed-off-by: brian m. carlson --- lib/newfol/filemanip.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/newfol/filemanip.py b/lib/newfol/filemanip.py index 0430392..c9a47b6 100644 --- a/lib/newfol/filemanip.py +++ b/lib/newfol/filemanip.py @@ -218,11 +218,10 @@ class HashTransactionStore(TransactionStore): @staticmethod def _hash_file(hashname, filename): h = hashlib.__dict__[hashname]() - fp = open(filename, "rb") - data = fp.read(-1) + with open(filename, "rb") as fp: + data = fp.read(-1) totallen = len(data) h.update(data) - fp.close() return "%s:%s:%s" % (hashname, str(totallen), h.hexdigest()) @staticmethod @@ -238,9 +237,8 @@ class HashTransactionStore(TransactionStore): self._mode = mode if "r" in mode: try: - fp = open(filename + ".checksum", "r") - line = fp.readline()[:-1] - fp.close() + with open(filename + ".checksum", "r") as fp: + line = fp.readline()[:-1] hashtype = line.split(":")[0] result = self._hash_file(self._get_hashtype(self._hashname, hashtype), @@ -258,9 +256,8 @@ class HashTransactionStore(TransactionStore): if "w" in self._mode: result = self._hash_file(self._get_hashtype(self._hashname, None), self._filename) - fp = open(self._filename + ".checksum", "w") - fp.write(result + "\n") - fp.close() + with open(self._filename + ".checksum", "w") as fp: + fp.write(result + "\n") class FileFormat: