问题
在python中创建压缩文件。
介绍
ZIP文件可以存储许多其他文件的压缩内容。 压缩文件会减少磁盘上的大小。 这在通过internet、使用控制- m aft、Connect direct和scp在系统之间传输时非常有用。
Python程序使用ZIPfile模块中的函数创建zip文件。
怎么做.
使用zipfile和io包。 如果系统中没有软件包,请使用pip进行安装。 如果不知道,请使用pip Frozen命令验证软件包。
2 .创建将示例数据写入文件的功能。 以下函数write_data_to_files使用数据作为输入,在当前目录名称中创建文件:
示例# function : write _ data _ to _ files
efwrite_data_to_files(InP_data,file_name ) :
''''
function : createacsvfilewiththedatapassedtothiscode
args : InP _ data : datatobewrittentothetargetfile
file _ name : targetfilenametostorethedata
return :无
assumption : filetobecreatedandthiscodeareinsamedirectory。
''''
print (f (* * *写入数据- { file _ name } ) )
throwaway _ storage=io.stringio (InP _ data ) )。
withopen(file_name,' w ' ) asf:
forlineinthrowaway _ storage :
f .写入(line )
3 .现在,创建一个函数file_compress,用于压缩在上述步骤中创建的文件。 此功能接受文件列表,遍历文件并将其压缩为zip文件。 有关每个步骤的详细信息,请参阅注释。
要创建自己的压缩ZIP文件,必须将第二个参数“w”传递到写入模式并打开ZipFile对象。
将路径传递给write(ZIPfile对象的方法)时,Python会使用该路径压缩文件并将其添加到zip文件中。
write ) ) method的第一个参数是要添加的文件名字符串。
第二个参数是压缩类型的参数。 我会告诉计算机使用哪个算法压缩文件。
示例#Function:file_compress
deffile _ compress (InP _ file _ names,out_zip_file ) :
''''
function:file_compress
args : InP _ file _ names 3360 listoffilenamestobezipped
out_zip_file:outputzipfile
return :无
assumption : inputfilepathsandthiscodeisinsamedirectory。
''''
# selectthecompressionmodezip _ deflatedforcompression
# or zipfile.zip _ storedtojuststorethefile
compression=zipfile.zip _ deflated
print (f (* * * inputfilenamepassedforzipping-{ InP _ file _ names } ' )
# createthezipfilefirstparameterpath/name,第二模式
print(f ) * * out _ zip _ fileis-{ out _ zip _ file } ) )
ZF=zipfile.zipfile (out _ zip _ file,mode='w ' ) )。
try:
for file _ to _ writeininp _ file _ names 3360
#Addfiletothezipfile
#firstparameterfiletozip,secondfilenameinzip
print (f (* * *处理文件)文件_ to _ write ) )
ZF.write(file_to_write,file_to_write,compress_type=compression ) ) ) ) ) ) ) )。
高速缓存
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 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
输出结果
执行以上代码后,输出为在当前目录中创建了secondary_file1_for_zip.csv。
在当前目录中创建了临时文件2_for_zip.csv。
在当前目录中创建了临时文件。