3

Пишу на Python. Требуется создать zip архив без некоторых каталогов и чтоб в архиве были только файлы данной директории. Пробовал использовать make_archive из shutil, но не нашел исключение.

1 ответ 1

3

Создайте сами список исключений

import zipfile
import os

# Находим все в текущей директории
currentDirectory = os.listdir(".")

# Создаем список файлов
list_files = []
for currentFile in currentDirectory:  
    list_files.append(currentFile)

# Удаляем ненужное
del_list = ["folder1", "folder2"]
for i in del_list:
    if i in currentDirectory:
        list_files.remove(i)

# Пишем в архив
with zipfile.ZipFile('arh.zip', 'w') as myzip:
    for i in list_files:
        for root, dirs, files in os.walk(i):
            for file in files:
                path = os.path.join(root, file)
                myzip.write(path)

3
  • Рекурсии не хватает или он сам папки упаковывает?
    – Qwertiy
    15 дек 2020 в 22:04
  • Да, нужна рекурсия
    – Dev
    16 дек 2020 в 8:58
  • Да будет нужна. Поправил ответ.
    – Kers
    16 дек 2020 в 10:48

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.