0

This is driving me crazy. a simple python script trying to zip contents of a folder 'X'. My problem is the zip file 'Y.zip' contains the sub files and subfolders of 'X' directly under it without the actual 'X' folder. What i expect is when i open 'Y.Zip' , i should see the folder 'X' and under it all the subfiles. How do i make it ?

def zippy():
    folders = os.listdir(os.getcwd())
    for each_folder in folders:
        if each_folder.endswith('X'):
            shutil.make_archive('Y', 'zip' )
4

1 回答 1

0

您是想同时压缩其中的文件和文件夹,还是只是制作一个包含起始文件夹和其中其他文件夹的 zip?这对我有用,可以将第一个文件夹及其所有内容压缩到 1 个 zip 中。

import shutil
import os

os.chdir(where the folder is)

shutil.make_archive('folder1', 'zip', 'where the folder is', 'folder1')
于 2020-10-28T13:54:04.373 回答