liveBook · Manning

Zipfile

As explained in Learn AI-Assisted Python Programming, Second Edition

The zipfile module is a Python standard-library module for creating, reading, writing, and extracting ZIP archives.

zipfile is a built-in Python module for organizing multiple files into one .zip archive, making files easier to back up, upload, and share. section 5.1.5

A typical archive task has three parts:

  • Prepare several CSV files, such as nfl_offensive_stats.csv, actors.csv, and chores.csv. section 5.1.5
  • Create my_stuff.zip, add each file, and close the archive. section 5.1.5
  • Use related functions to read or extract archived files when needed. section 5.1.5

Create an archive

import zipfile zf = zipfile.ZipFile('my_stuff.zip', 'w', zipfile.ZIP_DEFLATED) zf.write('nfl_offensive_stats.csv') zf.write('actors.csv') zf.write('chores.csv') zf.close() This creates one compressed archive containing the three CSV files. section 5.1.5

Table 5.2 Summary of commonly used Python modules
Module
Built-In
Description
csv
Yes
Aids in the reading, writing, and analysis of CSV files
zipfile
Yes
Aids in the creation and extraction of compressed zip archive files
matplotlib
No
Graphics library for plotting that serves as the basis of other graphics libraries and can offer high levels of customization
plotly
No
A graphics library used for creating interactive plots for the web
seaborn
No
A graphics library built on top of matplotlib that aids in creating high- quality plots more easily than matplotlib
pandas
No
A data processing library that specializes in data frames, which are analogous to spreadsheets
scikit-learn
No
Contains basic tools for machine learning (i.e., helping to learn from data and make predictions)
numpy
No
Offers highly efficient data processing
pygame
No
A game programming library that helps to build interactive, graphical games in Python
django
No
Web development library that aids in designing websites and web applications

Compare zipfile with other commonly used Python modules and see that it is built in, unlike several data and graphics libraries. section 5.1.5

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Demonstrates archive creation and file addition, while deferring broader function discovery and extraction practice.
Examples
Uses nfl_offensive_stats.csv, actors.csv, chores.csv, and my_stuff.zip as concrete archive artifacts.
What it assumes
Assumes basic Python files and imports while teaching that zipfile is immediately available as a built-in module.
What you can do after
Operate the zipfile module to create a compressed archive containing multiple CSV files.
How it connects
Connects Python modules, CSV files, file handling, and ZIP archives.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.