Using Python to compress files, you can use the zipfile library, which provides a very rich API
zipfile
itself is a context manager, and you can use with
. The following is a simple demo.
pack
import os
import zipfile
def file2zip(zip_file_name: str, file_names: list):
""" Compress and store files in multiple folders as zip
:param zip_file_name: /root/Document/test.zip
:param file_names: ['/root/user/doc/test.txt', ...]
:return:
"""
# Read-write mode ZipFile requires mode 'r', 'w', 'x', or 'a'
# Compression mode ZIP_STORED: stored; ZIP_DEFLATED: compressed storage
with zipfile.ZipFile(zip_file_name, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
for fn in file_names:
parent_path, name = os.path.split(fn)
# zipfile built-in provides for storing files compressed in .zip files, arcname is the name of the file stored in the zip file
# The given archive name is arcname (by default it will be the same as filename, but without the drive letter and will remove the opening path separator)
zf.write(fn, arcname=name)
# Equivalent to the following two lines of code
# Switch directories to write the file directly. Without switching directories, the entire path to the file is created in the zip file
# os.chdir(parent_path)
# zf.write(name)
if __name__ == "__main__":
zip_name = '/root/Document/test.zip'
files = ['/root/user/doc/test.txt', '/root/user/doc/test1.txt']
file2zip(zip_name , files)
decompression
def zip2file(zip_file_name: str, extract_path: str, members=None, pwd=None):
""" Folder specified by the zip file content extraction value
:param zip_file_name: the file to be extracted .zip r'D:\Desktop\tst\tst.zip'
:param extract_path: the directory where the extracted file is saved r'D:\Desktop\tst\test\test'
:param members: specify the files to extract, default all
:param pwd: the password to extract the file
:return:
"""
with zipfile.ZipFile(zip_file_name) as zf:
zf.extractall(extract_path, members=members, pwd=pwd)
Python file compression
zipfile. Pyzipfile
inherits from zipfile. Zipfile
, and has a special writepy
to package . Py
. PyC
Read More:
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- An introduction to sys modules in Python and how packages are imported and used
- [Solved] Failed to obtain/convert traceback after Python Tkinter packages exe
- Mac Upgrade pip Error OSError: [Errno 13] Permission denied: ‘/Library/Python/2.7/site-packages/pip-9.0.1-py2….
- [Solved] supervisor Error: /usr/local/lib/python2.7/dist-packages/pkg_resources/py2_warn.py:22: UserWarning: Setuptools will stop working on Python 2
- onnx error: ImportError: /home/dy/anaconda3/envs/torch/lib/python3.6/site-packages/onnx…
- Chromdriver Install error: File “C:\python37\lib\site-packages\selenium\webdriver\common\service.py”, line 76, in start stdin=PIPE) File…
- Python Error: ImportError: cannot import name ‘logsumexp’ from ‘scipy.misc’(Anaconda3\lib\site-packages\scipy\misc)
- [Solved] Error: unrecognized arguments: — no site packages
- [Solved] ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE
- Error:Could not install packages due to an OSError:[WinError 5] Access denied
- [Solved] ERROR: pip‘s dependency resolver does not currently take into account all the packages that are inst
- Python openpyxl excel open zipfile error resolution: zipfile.BadZipFile: File is not a zip file
- Python recursively traverses all files in the directory to find the specified file
- [How to Fix]RuntimeError: Python is not installed as a framework, If you are using (Ana)Conda
- SSL error of urllib3 when Python uploads files using Minio
- Python: How to Delete Empty Files or Folders in the Directory
- Python parses XML files (parses, updates, writes)
- How to Solve Python Pandas Read or Import Files Error
- Python: SVN deletes files on local and remote repositories