|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# @Time : 2022/2/26 16:55 |
| 4 | +# @Author : 一叶知秋 |
| 5 | +# @File : pack_zipfile.py |
| 6 | +# @Software: PyCharm |
| 7 | +from __future__ import print_function |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import zipfile |
| 11 | +import logging |
| 12 | + |
| 13 | +if sys.version_info[0] == 2: |
| 14 | + input = raw_input |
| 15 | +else: |
| 16 | + input = input |
| 17 | +formatter = '%(asctime)s %(levelname)s %(filename)s[%(lineno)d]: %(message)s' |
| 18 | +logging.basicConfig(level=logging.DEBUG, format=formatter, |
| 19 | + datefmt='%Y:%m:%d %H:%M:%S', encoding='utf-8') |
| 20 | + |
| 21 | + |
| 22 | +def make_zip_file(filename, sourcefolder): |
| 23 | + """ |
| 24 | + make zip file |
| 25 | + :param filename: zip file name |
| 26 | + :param sourceFolder: source file folder |
| 27 | + :return: |
| 28 | + """ |
| 29 | + with zipfile.ZipFile(filename, mode='w', compression=zipfile.ZIP_DEFLATED) as zip_file: |
| 30 | + for root, dirs, files in os.walk(sourcefolder): |
| 31 | + for file in files: |
| 32 | + fullpath = os.path.join(root, file) |
| 33 | + logging.info('filename is %s', fullpath) |
| 34 | + arcname = fullpath.replace(sourcefolder, '') |
| 35 | + logging.info('arcname is %s', arcname) |
| 36 | + zip_file.write(fullpath, arcname) |
| 37 | + |
| 38 | + |
| 39 | +def main(): |
| 40 | + sourceFolder = input('请输入源文件路径目录:\n') |
| 41 | + filename = input('请输入压缩文件名字: \n') |
| 42 | + logging.info('source file dir: %s', sourceFolder) |
| 43 | + logging.info('zip file name: %s', filename) |
| 44 | + make_zip_file(filename, sourceFolder) |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == '__main__': |
| 48 | + main() |
0 commit comments