mirror of
https://github.com/python/cpython.git
synced 2025-09-14 04:37:29 +00:00
packaging: use with open() instead of try/finally: close
This commit is contained in:
parent
0e3f3a7076
commit
21a9c748aa
11 changed files with 41 additions and 69 deletions
|
@ -18,14 +18,13 @@ from packaging.command.cmd import Command
|
|||
def zip_dir(directory):
|
||||
"""Compresses recursively contents of directory into a BytesIO object"""
|
||||
destination = BytesIO()
|
||||
zip_file = zipfile.ZipFile(destination, "w")
|
||||
for root, dirs, files in os.walk(directory):
|
||||
for name in files:
|
||||
full = os.path.join(root, name)
|
||||
relative = root[len(directory):].lstrip(os.path.sep)
|
||||
dest = os.path.join(relative, name)
|
||||
zip_file.write(full, dest)
|
||||
zip_file.close()
|
||||
with zipfile.ZipFile(destination, "w") as zip_file:
|
||||
for root, dirs, files in os.walk(directory):
|
||||
for name in files:
|
||||
full = os.path.join(root, name)
|
||||
relative = root[len(directory):].lstrip(os.path.sep)
|
||||
dest = os.path.join(relative, name)
|
||||
zip_file.write(full, dest)
|
||||
return destination
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue