This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

Retrospective review

Last time we looked at the Python file directory advanced operations -shutil module. The shutil module provides not only the ability to copy, move, and delete files/directories, but also the ability to archive, compress, and decompress files.

Before we start, let’s look at some of the confusing areas under the shutil module

⭐ question a: archives, compression and other nouns silly not clear?

  • Archiving: Also known as packaging.

    (1) Method: Merge multiple files into one file

    (2) Features: The archive file is not compressed, it takes up the space of all the files and directories combined

  • Unpack the:

    (1) Method: Release files in the file/directory collection (archive). (2) Features: The archiving operation is reverse

  • Compression:

    (1) Method: multiple files will be lossy or lossless merged into a file during compression.

    (2) Features: compressed files use different storage methods, it occupies less space than the sum of all files or directories

  • Extract:

    (1) Method: Release multiple files in the compressed file.

    (2) Features: it is the reverse operation of compression

🌈 Question 2: How many modules does Python provide for archive compression and decompression?

This time, we will mainly learn the common methods of Python’s built-in ZipFile module, and realize the operation methods of file compression and decompression. Without further talk, we will put on headphones 🎧 and play music 🎼, and start today’s learning journey 💃💃💃~

1. Common methods of zipFile module

The zipFile module is used to compress files, providing classes (new) and common methods as follows:

methods role
⭐ zipfile. Zipfile ZIP file read and write operations
⭐ zipfile. PyZipFile Create a ZIP archive containing the Python libraries
⭐ zipfile. ZipInfo One member information in the archive
zipfile.is_zipfile() Determines whether filename is a valid ZIP file and returns a Boolean value
zipfile.ZIP_STORED Represents a compressed archive member
zipfile.ZIP_DEFLATED Indicates a common ZIP compression method supported by the Zlib module
zipfile.ZIP_BZIP2 Indicates the BZIP2 compression method, which is supported by the BZ2 module
zipfile.ZIP_LZMA Indicates the LZMA compression method and must be supported by the LZMA module

📣 Important notes:

  • Zipfile class: Used to create and open zipfile objects

    (1) Can be used with the context manager with

    (2) zipfile. The zipFile object provides methods such as write(),read(),close(),extract(), etc

    The title role
    getinfo (filename) Return a ZipInfo object
    infolist() Returns the ZipInfo containing each compressed file
    namelist() Returns a list of compressed files sorted by file name
    open(filename) Access to an archive file as a binary file class for use with the context manager
    write(filename,arcname) Write the file to a compressed file
    read(filename) Returns the bytes of the compressed file
    printdir() Prints the directory in the compressed file to sys.stdout
    extract() Unzip a single compressed file
    extractall() Decompress multiple compressed files
  • Zipfile.PyZipFile: Create a ZIP archive containing Python libraries

    (1) The PyZipFile constructor is similar to the ZipFile constructor parameter, with the addition of the optimize parameter

    (2) The PyZipFile instance method has one more writepy() method than ZipFile

  • Zipfile. ZipInfo: One member information in the archive

    (1) Get the ZipInfo instance object by calling getinFoll from the ZipFile instance object

    (2) Information methods of members in the archive file, such as filename, date_time, and compress_type

2. Compress the zipFile module

📚 compression implementation steps:

  1. Create zipfile: zp = zipfile.zipfile ()

  2. Write (filename, arcname) Only files can be added. If a folder is added, an empty folder is compressed

  3. To close the compressed file: zp.close()

import zipfile

z = zipfile.ZipFile("test.zip","w")

z.write('test1.txt')
z.write(r'./New/New10_test.txt')
z.write(r'./old/old6_test/test.txt')



z.close()

print(z.printdir())
Copy the code

3. Decompress the zipFile module

📗 📘 📙 extract:

  1. Zp = zipfile.zipfile (zip_path, ‘r’)

  2. Decompress file operation:

    Zp. Extract (member, path)

    (2) extractall files: zp.extractall(path)

  3. Close the file: zp.close()

z = zipfile.ZipFile("test.zip","r")

z.extract('test1.txt','./zip')

z.extractall('./zip3')
Copy the code

conclusion

In this installment, we take a comprehensive look at concepts like archiving, compression, and the current Python module for compressing and decompressing files and directories.

At the same time, the classes and methods provided by zipFile module are learned and mastered, which is convenient to use in our daily work

The above is the content of this issue, welcome everyone big guy to like comment, see you next time ~