[英]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.