如何在Python中使用ZIPFILE模块压缩文件。
问题
您要在python中创建一个压缩文件。
介绍
ZIP文件可以保存许多其他文件的压缩内容。压缩文件会减小其在磁盘上的大小,这在通过Internet或使用Control-m AFT或Connect direct甚至scp在系统之间进行传输时非常有用。
Python程序使用zipfile模块中的函数创建ZIP文件。
怎么做...
1.我们将使用zipfile和io包。如果系统上缺少任何软件包,请使用pip安装它们。如果不确定,请使用pip Frozen命令验证软件包。
2.我们将编写一个将样本数据写入文件的功能。下面的函数write_data_to_files将数据作为输入,并在当前目录名称中创建一个文件。
示例
# Function : write_data_to_filesdef 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对象。
当您将路径传递给write()
ZipFile对象的方法时,Python会在该路径上压缩文件并将其添加到ZIP文件中。
write()
method的第一个参数是要添加的文件名的字符串。
第二个参数是压缩类型参数-告诉计算机应使用哪种算法压缩文件。
示例
# Function : file_compressdef 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大满贯的网球运动员的数据suspended_file1_for_zip.csv。然后,我们将这两个文件都压缩为临时文件。
示例
import zipfileimport 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
输出结果
执行以上代码后,输出为
在当前目录中创建了secondary_file1_for_zip.csv。
在当前目录中创建了临时文件2_for_zip.csv。
在当前目录中创建了临时文件。
以上是 如何在Python中使用ZIPFILE模块压缩文件。 的全部内容, 来源链接: utcz.com/z/348763.html