
- 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 we can compress large Python files?
We can use Python to compress or extract files. We use the zipfile module in Python, to extract or compress individual or multiple files at once. This process is is easy and requires very little code. We begin by importing the zipfile module and then open the ZipFile object in write mode by specifying the second parameter as 'w'. The file to be zipped is in the same folder as this code file or the path of the file to be zipped can be specified instead. Here is the code that you need −
import zipfile foo_zip = zipfile.ZipFile ( 'foo.zip', 'w' ) foo_zip.write ( 'foo.txt', compress_type=zipfile.ZIP_DEFLATED ) foo_zip.close ()
This creates zipped file foo.zip from the file foo.txt
- Related Articles
- How to Compress files with ZIPFILE module in Python.
- How we can split Python class into multiple files?
- Sponge is solid, but we can still compress it. Why?
- How can we transfer information between MySQL and data files?
- How to stream large .mp4 files in HTML5?
- How to download large files through PHP script?
- How to Handle Large CSV files with Pandas?
- Sending large files over internet
- How to upload large files above 500MB in PHP?
- How can we import CSV files into MySQL tables by using mysqlimport?
- How to compress Python objects before saving to cache?
- How can we transfer information between MySQL and data files through command line?
- How to split or break large files into pieces in Linux?
- The best way to compress and extract files using the tar command on linux
- How we can bundle multiple python modules?

Advertisements