Skip to content

Instantly share code, notes, and snippets.

@james-see
Created May 28, 2022 23:46
Show Gist options
  • Save james-see/7d9f45d26de8372ccc8d2b101eb660a7 to your computer and use it in GitHub Desktop.
Save james-see/7d9f45d26de8372ccc8d2b101eb660a7 to your computer and use it in GitHub Desktop.
zip files in a folder with status bar python
def package_tar(configpath="~/MATERIALS/", filename="compressed.zip") -> None:
"""zips up the container tars
Args:
configpath (str, optional): _description_. Defaults to "~/MATERIALS/".
filename (str, optional): _description_. Defaults to "compressed.zip".
"""
import zipfile
import shutil
input_dir = Path(configpath)
totalrange = len(list(input_dir.glob("*")))
# with zipfile.ZipFile(output_filename_fullpath, "w", zipfile.ZIP_DEFLATED) as zip_file:
with zipfile.ZipFile(f"/tmp/{filename}", "w", zipfile.ZIP_DEFLATED) as archive:
for counter, file_path in enumerate(input_dir.iterdir()):
archive.write(file_path, arcname=file_path.name)
print(end="\r|%-80s|" % ("=" * int(80 * counter / (totalrange - 1))))
shutil.move(f"/tmp/{filename}", configpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment