You can download this code by clicking the button below.
This code is now available for download.
This function compresses two files into a zip file, including a timestamp in the filename, and returns the name of the generated zip file.
Technology Stack : zipfile, os, shutil, datetime
Code Type : Function
Code Difficulty : Advanced
def zip_file(file1, file2):
import os
import shutil
import zipfile
import datetime
# 创建一个zip文件
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
zip_filename = f"{file1}_{file2}_{timestamp}.zip"
with zipfile.ZipFile(zip_filename, 'w') as zipf:
# 添加文件1到zip文件
zipf.write(file1, os.path.basename(file1))
# 添加文件2到zip文件
zipf.write(file2, os.path.basename(file2))
return zip_filename