有趣生活

当前位置:首页>科技>python中zip的用法Python入门题048文件夹生成zip

python中zip的用法Python入门题048文件夹生成zip

发布时间:2026-07-21阅读(1)

导读使用shutil或zipFile对整个文件夹进行压缩,生成一个zip压缩包,今天小编就来聊一聊关于python中zip的用法?接下来我们就一起去研究一下吧!....

使用 shutil 或 zipFile 对整个文件夹进行压缩,生成一个 zip 压缩包,今天小编就来聊一聊关于python中zip的用法?接下来我们就一起去研究一下吧!

python中zip的用法(Python入门题048文件夹生成zip)

python中zip的用法

题目:

使用 shutil 或 zipFile 对整个文件夹进行压缩,生成一个 zip 压缩包

#python #zip #shutil #压缩包

视频教程:

Python入门题048:文件夹生成zip(用shutil或zipfile)

代码1:

import shutildirname = /private/var/www/coding-anderson/Python_100_Exercises/exercises_031_to_040shutil.make_archive(my_file, zip, dirname)

代码2:

import osimport zipfiledirname = /private/var/www/coding-anderson/Python_100_Exercises/exercises_031_to_040def zipdir(path, ziph: zipfile.ZipFile): # ziph is zipfile handle for root, dirs, files in os.walk(path): for file in files: if not file.endswith(.py): continue abspath = os.path.join(root, file) ziph.write(abspath, # 必须传入相对路径, # 否则压缩包里的文件夹是从 /private 开始的 os.path.relpath(abspath, os.path.join(path, ..)))zipf = zipfile.ZipFile(my_file.zip, w, zipfile.ZIP_DEFLATED)zipdir(dirname, zipf)zipf.close()

TAGS标签:  python  用法  入门  文件夹  python中zip

Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图