美文网首页
python zipfile

python zipfile

作者: hehehehe | 来源:发表于2023-12-12 21:06 被阅读0次
    import os
    from os.path import basename
    from pathlib import Path
    import zipfile
    
    
    def zip_files(zip_file, dir):
        with zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED) as z:
            for filename in os.listdir(dir):
                if not filename.endswith(".zip"):
                    z.write(f"{dir}/{filename}", arcname=basename(filename))
    
    
    def make_dir(path):
        if not Path(path).exists():
            Path(path).mkdir(parents=True, exist_ok=True)
    
    
    if __name__ == '__main__':
        # zip_files(f"/Users/shilulu/Downloads/data/postdata/z.zip",
        #           f"/Users/shilulu/Downloads/data/postdata/")
        path = "/chj/sll/data/postdata/st_hdmap_main"
        path_out = "/chj/sll/data/postdata/hdmap"
        for city in os.listdir(path):
            zip_files(f"{path_out}/{city}.zip", f"{path}/{city}/shp")
        #     break
    
    

    shutil.make_archive

    import shutil
    
    # 指定要压缩的文件夹路径
    folder_path = 'path_to_directory_to_compress'
    # 指定输出的ZIP文件名(不需要添加.zip后缀)
    output_filename = 'output_archive'
    
    # 调用make_archive函数进行压缩
    shutil.make_archive(output_filename, 'zip', folder_path)
    
    print(f"Folder '{folder_path}' has been compressed as '{output_filename}.zip'.")
    

    相关文章

      网友评论

          本文标题:python zipfile

          本文链接:https://www.haomeiwen.com/subject/rrtbgdtx.html