python zip_files.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python zip_files.py相关的知识,希望对你有一定的参考价值。

#coding: utf-8

"""Throwaway script.

Usage:
  test.py
  test.py zip <project>...
  
"""
import os, sys
from docopt import docopt
import zipfile
import re
#%%
_dir = os.path.dirname(__file__)

if __name__ == '__main__':
    args = docopt(__doc__)
    
    if args['zip']:
        ignore_files = [r'\.pyc']
        
        for path in args['<project>']:
            zip_path = os.path.join(_dir, 'test_projects',
                                    os.path.basename(path) + '.zip')
            with zipfile.ZipFile(zip_path, 'w') as _zip:
#            with zipfile.PyZipFile(zip_path, 'w') as _zip:    
#                for name in os.listdir(path):
#                    filepath = os.path.join(path, name)
#                    if os.path.isfile(filepath) and not name.endswith('.py'):
#                        continue
#                    _zip.writepy(filepath)
                for current_path, dirs, files in os.walk(path):
                    for name in files:
                        if any(re.search(regexp, name) for regexp in ignore_files):
                            continue
                        relpath = os.path.relpath(current_path, path)
                        _zip.write(os.path.join(current_path, name),
                                   os.path.join(relpath, name))

以上是关于python zip_files.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python