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


问题

你想在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个或少于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文件已创建在当前目录中。

更新于:2020年11月9日

11K+ 浏览量

启动您的职业生涯

完成课程获得认证

开始学习
广告