Skip to content

Commit

Permalink
Applied context manager to sub zip file merging
Browse files Browse the repository at this point in the history
  • Loading branch information
Karko93 committed Dec 19, 2023
1 parent db3d3c2 commit d420b4e
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/pyflexplot/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ def main(
)
except FileNotFoundError:
log(err="Error merging shape files.")

for path in redundant_shape_files:
if not dry_run:
log(dbg=f"remove {path}")
Path(path).unlink()
else:
for path in redundant_shape_files:
if not dry_run:
log(dbg=f"remove {path}")
Path(path).unlink()

# Remove temporary directory (if given) unless it already existed before
remove_tmpdir = tmp_dir and not dry_run and not os.listdir(tmp_dir)
Expand Down Expand Up @@ -519,10 +519,12 @@ def merge_shape_files(
if not dry_run:
with zipfile.ZipFile(tmp_zip_name, "w") as main_zip:
for file in group:
zip_to_merge = zipfile.ZipFile(file, "r")
n += len(zip_to_merge.namelist())
for sub_file in zip_to_merge.namelist():
main_zip.writestr(sub_file, zip_to_merge.open(sub_file).read())
with zipfile.ZipFile(file, "r") as zip_to_merge:
n += len(zip_to_merge.namelist())
for sub_file in zip_to_merge.namelist():
main_zip.writestr(
sub_file, zip_to_merge.open(sub_file).read()
)
shutil.copy(tmp_zip_name, merged)
os.remove(tmp_zip_name)
for path in group:
Expand Down

0 comments on commit d420b4e

Please sign in to comment.