(dir_path, zip_name, ignore_dirs: list = None, ignore_files: list = None)
| 22 | |
| 23 | |
| 24 | def zip(dir_path, zip_name, ignore_dirs: list = None, ignore_files: list = None): |
| 25 | print(f"正在压缩 {dir_path} >> {zip_name}") |
| 26 | ignore_files.append(os.path.basename(zip_name)) |
| 27 | with zipfile.ZipFile(zip_name, "w") as file: |
| 28 | dir_name = os.path.basename(dir_path) |
| 29 | parent_dir = os.path.dirname(dir_path) |
| 30 | if parent_dir: |
| 31 | os.chdir(parent_dir) |
| 32 | for path, dirs, filenames in os.walk(dir_name): |
| 33 | # 修改原dirs,方式遍历忽略文件夹里的文件 |
| 34 | if ignore_dirs: |
| 35 | dirs[:] = [d for d in dirs if d not in ignore_dirs] |
| 36 | for filename in filenames: |
| 37 | if ignore_files and is_ignore_file(ignore_files, filename): |
| 38 | continue |
| 39 | |
| 40 | filepath = os.path.join(path, filename) |
| 41 | print(f" adding {filepath}") |
| 42 | file.write(filepath) |
| 43 | |
| 44 | print(f"压缩成功 {dir_path} >> {zip_name}") |
| 45 | |
| 46 | |
| 47 | def parse_args(): |
no test coverage detected