How To Zip Files In Python


  • important: by default, the module only stores and does not compress, this has to be explicitly set
  • using this for small files (deployment logs) and therefore opting for the highest compression levels (performance considerations are a non-issue for me)
  • to sum this, you need 4 args
  • mode="w"
  • compression method=ZIP_DEFLATED
  • compression_level=9
  • the bare-boned syntax you can test from the python REPL would be (I am compressing
from zipfile import ZIP_DEFLATED, ZipFile as zip
with zip("", mode="w",compression=ZIP_DEFLATED,compresslevel=9) as archive:


  • the example is a particular function from a script that automates deployments and compresses output.txt into a filename containing information about a particular deployment (Jira card, deployed component, git user name of a deployer, timestamp) and also automatically pushes that to GitHub




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: