您的位置 首页 > 数码极客

zip命令如何排除某个路径

一、python封装的使用zip压缩文件的代码(修正版):

说明:这个函数只是针对性地压缩一个目录下所有文件,若使用,你可能需要再修改。

作用:压缩目录并返回压缩文件路径。

参数:

zip_filename str: 压缩文件名,包含后缀.zip

zip_path str: 压缩目录,程序要能探查到目录,比如可以用绝对路径;或者相对路径,但是要切换到同目录下

exclude list: 依据后缀要排除的文件,参考使用中的例子

使用:

zipfilepath = make_zipfile(";, "testdir", [".zip", ".lock"])

以上代码运行的当前目录存在testdir目录,将会逐一压缩testdir目录下的文件,文件后缀包含.zip、.lock的将不压缩。

代码:

def make_zipfile(zip_filename, zip_path, exclude=[]): """Create a zipped file with the name zip_filename. Compress the files in the zip_path directory. Do not include subdirectories. Exclude files in the exclude file. @param zip_filename str: Compressed file name @param zip_path str: The compressed directory (the files in this directory will be compressed) @param exclude list,tuple: File suffixes will not be compressed in this list when compressed """ if zip_filename and os.(zip_filename)[-1] == ".zip" and zip_path and os.(zip_path) and isinstance(exclude, (list, tuple)): try: import zipfile except ImportError: raise with zi(zip_filename, "w", compression=zi, allowZip64=True) as zf: for filename in os.listdir(zip_path): if os.(filename): continue if not os.(filename)[-1] in exclude: zf.write(zip_path, filename), filename) return zip_filename if os.(zip_filename) else os.(), zip_filename) else: raise TypeError

二、遇到的问题:

1. LargeZipFile: Zipfile size would require ZIP64 extensions

答:参加上述代码,allowZip64=True在这个问题发生时是没有的,由于压缩容量过大,发生异常,此时需要允许zip64,这个参数默认是False,改为True可以压缩成过大文件。

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“zip命令如何排除某个路径”边界阅读