diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-02-02 20:30:22 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-02-02 20:30:22 (GMT) |
commit | 091d386f7877ee1a37475dddc6ad845f5b70e4a2 (patch) | |
tree | 0de7153d04f7c4adc1c66a52e2237bd08dc9155d | |
parent | c8d9e0e932551b2722e1db3ced611e1c3c078cc7 (diff) | |
download | cpython-091d386f7877ee1a37475dddc6ad845f5b70e4a2.zip cpython-091d386f7877ee1a37475dddc6ad845f5b70e4a2.tar.gz cpython-091d386f7877ee1a37475dddc6ad845f5b70e4a2.tar.bz2 |
use with statement to ensure zipfile is always closed (closes #20102)
-rw-r--r-- | Lib/shutil.py | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index a5b2d24..5320f23 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -680,17 +680,15 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None): zip_filename, base_dir) if not dry_run: - zip = zipfile.ZipFile(zip_filename, "w", - compression=zipfile.ZIP_DEFLATED) - - for dirpath, dirnames, filenames in os.walk(base_dir): - for name in filenames: - path = os.path.normpath(os.path.join(dirpath, name)) - if os.path.isfile(path): - zip.write(path, path) - if logger is not None: - logger.info("adding '%s'", path) - zip.close() + with zipfile.ZipFile(zip_filename, "w", + compression=zipfile.ZIP_DEFLATED) as zf: + for dirpath, dirnames, filenames in os.walk(base_dir): + for name in filenames: + path = os.path.normpath(os.path.join(dirpath, name)) + if os.path.isfile(path): + zf.write(path, path) + if logger is not None: + logger.info("adding '%s'", path) return zip_filename |