zipfile (compressed file)

About

zipfile module for processing the file compression and decompression operations.

Compressed Folders

import zipfile  # 导入模块
# BASE_STATIC_CASE_RESULT:我Django static下面的某个路径
base_dir = os.path.join(BASE_STATIC_CASE_RESULT, 'temp')  # 要压缩文件夹的根路径
zip_file_name = 'temp.zip'
f = zipfile.ZipFile(os.path.join(BASE_STATIC_CASE_RESULT, zip_file_name), 'w', zipfile.ZIP_DEFLATED)
for dir_path, dir_name, file_names in os.walk(base_dir):
    # 要是不replace,就从根目录开始复制
    file_path = dir_path.replace(base_dir, '')
    # 实现当前文件夹以及包含的所有文件
    file_path = file_path and file_path + os.sep or ''
    for file_name in file_names:
        f.write(os.path.join(dir_path, file_name), file_path + file_name)
f.close()

Since there is no study the source code, for the time being they are fixed when writing it.

Create a compressed file

import zipfile
def zip_files( files, zip_name ):
    zip = zipfile.ZipFile( zip_name, 'w', zipfile.ZIP_DEFLATED )
    for file in files:
        print ('compressing', file)
        zip.write( file )
    zip.close()
    print ('compressing finished')
files = ['.\\123.txt','.\\3.txt']#文件的位置,多个文件用“,”隔开
zip_file = '.\\m66y.zip'#压缩包名字
zip_files(files, zip_file)

All's that
See Also: [Python] using Python compressed file / folder | using Python to read and write and to compress and decompress files example

About

zipfile module for processing the file compression and decompression operations.

Compressed Folders

import zipfile  # 导入模块
# BASE_STATIC_CASE_RESULT:我Django static下面的某个路径
base_dir = os.path.join(BASE_STATIC_CASE_RESULT, 'temp')  # 要压缩文件夹的根路径
zip_file_name = 'temp.zip'
f = zipfile.ZipFile(os.path.join(BASE_STATIC_CASE_RESULT, zip_file_name), 'w', zipfile.ZIP_DEFLATED)
for dir_path, dir_name, file_names in os.walk(base_dir):
    # 要是不replace,就从根目录开始复制
    file_path = dir_path.replace(base_dir, '')
    # 实现当前文件夹以及包含的所有文件
    file_path = file_path and file_path + os.sep or ''
    for file_name in file_names:
        f.write(os.path.join(dir_path, file_name), file_path + file_name)
f.close()

Since there is no study the source code, for the time being they are fixed when writing it.

Create a compressed file

import zipfile
def zip_files( files, zip_name ):
    zip = zipfile.ZipFile( zip_name, 'w', zipfile.ZIP_DEFLATED )
    for file in files:
        print ('compressing', file)
        zip.write( file )
    zip.close()
    print ('compressing finished')
files = ['.\\123.txt','.\\3.txt']#文件的位置,多个文件用“,”隔开
zip_file = '.\\m66y.zip'#压缩包名字
zip_files(files, zip_file)

All's that
See Also: [Python] using Python compressed file / folder | using Python to read and write and to compress and decompress files example

Guess you like

Origin www.cnblogs.com/qq834761298/p/12163926.html