#!/usr/bin/env python3
# Version = 3.8.1
"""
压缩共享文件访问日志
date: 2020-01-08
Author:lumia98@vip.qq.com
"""
from zipfile import ZipFile
import zipfile,os
def zipfiles(localzip, dirpath):
for path, dirnames, filenames in os.walk(dirpath):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(dirpath, '')
for file in filenames:
#循环按照源文件名压缩文件
path_file = os.path.join(localzip, file)
zip = zipfile.ZipFile(path_file+'.zip', "w", zipfile.ZIP_DEFLATED)
#切割列表
splist_file = file.split('.')[0]
#切割的字符串相等则退出循环,因为不需要压缩Security这个文件
if splist_file != "Security":
#开始压缩
zip.write(os.path.join(path, file), os.path.join(fpath, file))
zip.close()
#压缩完成后删除文件
remove_file = os.path.join(dirpath, file)
os.remove(remove_file)
zipfiles("/mnt/securitydir/BackupSecurity", "/mnt/securitydir/Security")
# 备份日志的目录: /mnt/securitydir/BackupSecurity
# 需要压缩的日志目录: /mnt/securitydir/Security
Python压缩程序日志方便备份
最新推荐文章于 2022-07-25 11:35:37 发布