From d0b5f254bb81358a76933b062ae2dda1cdc30cb7 Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Wed, 20 Feb 2013 21:16:47 +0100 Subject: [PATCH] Use with statement. --- patoolib/programs/py_bz2.py | 10 ++-------- patoolib/programs/py_gzip.py | 10 ++-------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/patoolib/programs/py_bz2.py b/patoolib/programs/py_bz2.py index e84796d..4d30377 100644 --- a/patoolib/programs/py_bz2.py +++ b/patoolib/programs/py_bz2.py @@ -32,14 +32,11 @@ def extract_bzip2 (archive, compression, cmd, **kwargs): targetname = util.get_single_outfile(outdir, archive) bz2file = bz2.BZ2File(archive) try: - targetfile = open(targetname, 'wb') - try: + with open(targetname, 'wb') as targetfile: data = bz2file.read(READ_SIZE_BYTES) while data: targetfile.write(data) data = bz2file.read(READ_SIZE_BYTES) - finally: - targetfile.close() finally: bz2file.close() if verbose: @@ -57,16 +54,13 @@ def create_bzip2 (archive, compression, cmd, *args, **kwargs): bz2file = bz2.BZ2File(archive, 'wb') try: filename = args[0] - srcfile = open(filename) - try: + with open(filename) as srcfile: data = srcfile.read(READ_SIZE_BYTES) while data: bz2file.write(data) data = srcfile.read(READ_SIZE_BYTES) if verbose: util.log_info('... added %s' % filename) - finally: - srcfile.close() finally: bz2file.close() return None diff --git a/patoolib/programs/py_gzip.py b/patoolib/programs/py_gzip.py index debae30..0878927 100644 --- a/patoolib/programs/py_gzip.py +++ b/patoolib/programs/py_gzip.py @@ -30,14 +30,11 @@ def extract_gzip (archive, compression, cmd, **kwargs): targetname = util.get_single_outfile(outdir, archive) gzipfile = gzip.GzipFile(archive) try: - targetfile = open(targetname, 'wb') - try: + with open(targetname, 'wb') as targetfile: data = gzipfile.read(READ_SIZE_BYTES) while data: targetfile.write(data) data = gzipfile.read(READ_SIZE_BYTES) - finally: - targetfile.close() finally: gzipfile.close() if verbose: @@ -55,16 +52,13 @@ def create_gzip (archive, compression, cmd, *args, **kwargs): gzipfile = gzip.GzipFile(archive, 'wb') try: filename = args[0] - srcfile = open(filename) - try: + with open(filename) as srcfile: data = srcfile.read(READ_SIZE_BYTES) while data: gzipfile.write(data) data = srcfile.read(READ_SIZE_BYTES) if verbose: util.log_info('... added %s' % filename) - finally: - srcfile.close() finally: gzipfile.close() return None