文件夹压缩 zip
import zipfile
srmdir_all_folder # 文件夹路径
print('原始文件夹路径: ', srmdir_all_folder)
zip_name = 'srmdir_all.zip' # 压缩文件名称
zip_file_path = srmdir_all_folder + '.zip' # 压缩文件路径
zip_file = zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) # 创建空的 zip文件
zip_file.write(srmdir_all_folder, 'srmdir_all')
for dirpath, dirnames, filenames in os.walk(srmdir_all_folder):
fpath = dirpath.replace(srmdir_all_folder, '') # 获取 相对文件夹的路径
fpath = fpath and fpath + os.sep or os.sep # 添加 '/'
for filename in filenames:
if filename.endswith('.txt'):
zip_file.write(os.path.join(dirpath, filename), 'srmdir_all' + fpath + filename)
zip_file.close()
标签:zip,Python,fpath,srmdir,文件夹,file,folder
From: https://www.cnblogs.com/jessecheng/p/16924358.html