如何使用 Python 中的 ZIPFILE 模块压缩文件。

pythonserver side programmingprogramming更新于 2026/2/14 20:28:17

问题

您想在 Python 中创建一个压缩文件。

简介

ZIP 文件可以保存许多其他文件的压缩内容。压缩文件会减小其在磁盘上的大小,这在使用 Control-m AFT 或 Connect direct 甚至 scp 通过互联网或系统之间传输文件时非常有用。

Python 程序使用 zipfile 模块中的函数创建 ZIP 文件。

如何操作...

1. 我们将使用 zipfile 和 io 包。如果您的系统上缺少任何包,请使用 pip 安装它们。如果您不确定,请使用 pip freeze 命令验证包。

2. 我们将编写一个函数将示例数据写入文件。下面的函数 write_data_to_files 将数据作为输入并在当前目录名中创建一个文件。

示例

# Function : write_data_to_files
def write_data_to_files(inp_data, file_name):
"""
function : create a csv file with the data passed to this code
args : inp_data : data to be written to the target file
file_name : target file name to store the data
return : none
assumption : File to be created and this code are in same directory.
"""
print(f" *** Writing the data to - {file_name}")
throwaway_storage = io.StringIO(inp_data)
with open(file_name, 'w') as f:
for line in throwaway_storage:
f.write(line)

3. 我们现在将编写一个 file_compress 函数来压缩上述步骤中创建的文件。此函数接受文件列表,浏览它们并将其压缩为 zip 文件。注释中提供了每个步骤的详细说明。

要创建自己的压缩 ZIP 文件,您必须通过传递 'w' 作为第二个参数以写入模式打开 ZipFile 对象。

当您将路径传递给 ZipFile 对象的 write() 方法时,Python 将压缩该路径上的文件并将其添加到 ZIP 文件中。

write() 方法的第一个参数是要添加的文件名的字符串。

第二个参数是压缩类型参数 - 它告诉计算机应该使用什么算法来压缩文件。

示例

# Function : file_compress
def file_compress(inp_file_names, out_zip_file):
"""
function : file_compress
args : inp_file_names : list of filenames to be zipped
out_zip_file : output zip file
return : none
assumption : Input file paths and this code is in same directory.
"""
# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
compression = zipfile.ZIP_DEFLATED
print(f" *** Input File name passed for zipping - {inp_file_names}")

# create the zip file first parameter path/name, second mode
print(f' *** out_zip_file is - {out_zip_file}')
zf = zipfile.ZipFile(out_zip_file, mode="w")

try:
for file_to_write in inp_file_names:
# Add file to the zip file
# first parameter file to zip, second filename in zip
print(f' *** Processing file {file_to_write}')
zf.write(file_to_write, file_to_write, compress_type=compression)

except FileNotFoundError as e:
print(f' *** Exception occurred during zip process - {e}')
finally:
# Don't forget to close the file!
zf.close()

4. 我们将调用函数来创建两个 csv 文件,然后对它们进行压缩。我们将使用赢得超过 1 个大满贯冠军的网球运动员数据到一个文件 - temporary_file1_for_zip.csv,将赢得少于或等于 1 个大满贯冠军的网球运动员数据到另一个文件 temporary_file1_for_zip.csv。然后我们将这两个文件压缩到 temporary.zip 文件中。

示例

import zipfile
import io
import pandas as pd

file_name1 = "temporary_file1_for_zip.csv"
file_name2 = "temporary_file2_for_zip.csv"
file_name_list = [file_name1, file_name2]
zip_file_name = "temporary.zip"

# data for file 1
file_data_1 = """
player,titles
Federer,20
Nadal,20
Djokovic,17
Murray,3
"""

# data for file 2

file_data_2 = """
player,titles
Theim,1
Zverev,0
Medvedev,0
Rublev,0
"""

# write the file_data to file_name
write_data_to_files(file_data_1, file_name1)
write_data_to_files(file_data_2, file_name2)

# zip the file_name to zip_file_name
file_compress(file_name_list, zip_file_name)

示例

5.将上述步骤中讨论的所有内容放在一起。

# Define the data
# let us create a zip file with a single file

import zipfile
import io
import pandas as pd

# Function : write_data_to_files
def write_data_to_files(inp_data, file_name):
"""
function : create a csv file with the data passed to this code
args : inp_data : data to be written to the target file
file_name : target file name to store the data
return : none
assumption : File to be created and this code are in same directory.
"""
print(f" *** Writing the data to - {file_name}")
throwaway_storage = io.StringIO(inp_data)
with open(file_name, 'w') as f:
for line in throwaway_storage:
f.write(line)

# Function : file_compress
def file_compress(inp_file_names, out_zip_file):
"""
function : file_compress
args : inp_file_names : list of filenames to be zipped
out_zip_file : output zip file
return : none
assumption : Input file paths and this code is in same directory.
"""
# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
compression = zipfile.ZIP_DEFLATED
print(f" *** Input File name passed for zipping - {inp_file_names}")

# create the zip file first parameter path/name, second mode
print(f' *** out_zip_file is - {out_zip_file}')
zf = zipfile.ZipFile(out_zip_file, mode="w")

try:
for file_to_write in inp_file_names:
# Add file to the zip file
# first parameter file to zip, second filename in zip
print(f' *** Processing file {file_to_write}')
zf.write(file_to_write, file_to_write, compress_type=compression)

except FileNotFoundError as e:
print(f' *** Exception occurred during zip process - {e}')
finally:
# Don't forget to close the file!
zf.close()

# __main__ program
if __name__ == '__main__':
# Define your file name and data
file_name1 = "temporary_file1_for_zip.csv"
file_name2 = "temporary_file2_for_zip.csv"

file_name_list = [file_name1, file_name2]
zip_file_name = "temporary.zip"
file_data_1 = """
player,titles
Federer,20
Nadal,20
Djokovic,17
Murray,3
"""

file_data_2 = """
player,titles
Theim,1
Zverev,0
Medvedev,0
Rublev,0
"""
# write the file_data to file_name
write_data_to_files(file_data_1, file_name1)
write_data_to_files(file_data_2, file_name2)

# zip the file_name to zip_file_name
file_compress(file_name_list, zip_file_name)
*** Writing the data to - temporary_file1_for_zip.csv
*** Writing the data to - temporary_file2_for_zip.csv
*** Input File name passed for zipping - ['temporary_file1_for_zip.csv', 'temporary_file2_for_zip.csv']
*** out_zip_file is - temporary.zip
*** Processing file temporary_file1_for_zip.csv
*** Processing file temporary_file2_for_zip.csv

输出

执行上述代码时,输​​出为

  • 在当前目录中创建temporary_file1_for_zip.csv。

  • 在当前目录中创建temporary_file2_for_zip.csv。

  • 在当前目录中创建temporary.zip文件。


相关文章


有用资源