简体   繁体   中英

zip a folder with all its content without preserving directory structure in Python

I have directory and has many csv files in it. When i try to zip the folder using zipfile or shutil package, then a complete directory structure is getting created. I just need to get the folder with lot of csv files zipped.

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")

In my Documents folder i have my_folder with all the csv files inside it. I zip it and drop it to my desktop named as data.zip . When you unzip it will be my_folder/all your csv files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM