
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How are files added to a zip file using Python?
Use the zipfile module to create a zip archive of a directory. Walk the directory tree using os.walk and add all the files in it recursively.
example
import os import zipfile def zipdir(path, ziph): # ziph is zipfile handle for root, dirs, files in os.walk(path): for file in files: ziph.write(os.path.join(root, file)) zipf = zipfile.ZipFile('Zipped_file.zip', 'w', zipfile.ZIP_DEFLATED) zipdir('./my_folder', zipf) zipf.close()
The above code will zip the contents of my_folder in a file 'Zipped_file.zip'. and store it in the current directory.
- Related Articles
- How are files added to a tar file using Python?
- How to extract all the .txt files from a zip file using Python?
- How to create a zip file using Python?
- Python Program to Read and printing all files from a zip file
- How are files extracted from a tar file using Python?
- How to Zip / Unzip files or folders using PowerShell?
- How to merge multiple files into a new file using Python?
- How to concatenate two files into a new file using Python?
- How to spilt a binary file into multiple files using Python?
- Working with zip files in Python
- Golang program to read and print all files from zip file
- How to zip a folder recursively using Python?
- How to create a zip archive of a directory using Python?
- How to save files using a File Chooser in JavaFX?
- How to open multiple files using a File Chooser in JavaFX?

Advertisements