Home >Backend Development >Python Tutorial >How to compress/decompress zip files using Python? (code example)

How to compress/decompress zip files using Python? (code example)

青灯夜游
青灯夜游Original
2019-02-18 10:35:0028139browse

It is very convenient to use zip files when exchanging large files and multiple files in batches. The following article will introduce you to zip files and introduce how to use Python to compress or decompress zip files. I hope it will be helpful to you. [Video tutorial recommendation: Python tutorial]

How to compress/decompress zip files using Python? (code example)

What is a zip file?

A zip file is a file compressed using a data compression format called zip. Data compression expresses data in a shorter format according to certain rules. For example, if "aaaaaaaaaa" is expressed with "a10", it can be shortened by 7 characters.

Zip is a universal format for such compression/decompression rules, which is used as a standard feature on Windows and Mac OS and can also be used with the zipfile standard library in Python.

Using zip, we can compress and decompress one or more files and folders. By compressing it into a zip file, large-capacity files can be distributed in small sizes, and complex-structured files and folders can be delivered centrally.

In addition to zip, data compression formats include rar, tar.gz, 7z, etc., but zip is the most widely used.

How to compress or decompress zip files using Python?

To compress or decompress zip files using python, we need to use a built-in python module called zipfile.

How to compress zip files using Python?

import zipfile
with zipfile.ZipFile(zip文件名, "w", zipfile.ZIP_DEFLATED) as zf:
  zf.write(要压缩的文件名,zip文件中的文件名)

Note:

The zipfile library can be imported and used through the import keyword. Lines starting with with indicate that zip files are to be processed. If you want to compress, you need to fill in the zip file name in the first parameter to create a zip file, specify "w" in the second parameter, and the third parameter is the compression format of the zip file.

Note: To create a regular zip file, you need to enter zipfile.ZIP_DEFLATED; if omitted, ZIP_STORED will be specified by default (no compression). In addition, there are the following categories:

● zipfile.ZIP_STORED: No compression. Produce multiple files at one time (default)

● zipfile.ZIP_DEFLATED: General zip compression

● zipfile.ZIP_BZIP 2: BZIP 2 compression

● zipfile.ZIP_LZMA: LZMA compression

How to decompress zip files using Python?

import zipfile
 with zipfile.ZipFile(zip文件名) as zf:
   zf.extractall()

Description:

In the case of decompression, you can import and use the zipfile library through the import keyword, and specify the target zip file name on the line starting with with; Decompress specified extractall method.

Examples of using Python to compress or decompress zip files

Let’s take an example to see how to use Python to compress/decompress Compress the zip file and confirm how the zipfile is written.

First we need to create a file to be compressed.

hello.txt

你好!
欢迎来到PHP中文网,学习Python!

Next we look at how to compress.

zip.py

import zipfile
with zipfile.ZipFile('hello.zip', "w", zipfile.ZIP_DEFLATED) as zf:
    zf.write("hello.txt","hello.txt")

When executed, a "hello.zip" file containing the "hello.txt" file will be created in the same folder as the program .

Next let’s look at how to decompress.

unzip.py

import zipfile
with zipfile.ZipFile('./ hello.zip') as zf:
  zf.extractall()

Instructions: First rename the original "Hello.txt" file and then run it. After running unzip.py, a "hello.txt" file appears in the same folder as the program after decompression.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to compress/decompress zip files using Python? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn