![](/img/trans.png)
[英]How to zip a folder and all of its files in Python, while preserving the folder name and relative paths for its contents?
[英]zip a folder with all its content without preserving directory structure in Python
我有目录,里面有很多 csv 文件。 当我尝试使用 zipfile 或 shutil package 来 zip 文件夹时,就会创建一个完整的目录结构。 我只需要获取包含大量 csv 压缩文件的文件夹。
import shutil
import os
def make_archive(source, destination):
base = os.path.basename(destination)
name = base.split(".")[0]
format = base.split(".")[1]
archive_from = os.path.dirname(source)
archive_to = os.path.basename(source.strip(os.sep))
shutil.make_archive(name, format, archive_from, archive_to)
shutil.move("%s.%s" % (name, format), destination)
make_archive("/Users/myusername/Documents/my_folder", "/Users/myusername/Desktop/data.zip")
在我的 Documents 文件夹中,我有my_folder
,其中包含所有 csv 文件。 我将 zip 放到我的桌面上,命名为data.zip
。 当您解压缩时,它将是my_folder/all your csv files
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.