Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude a directory from getting zipped using zipfile module in python

I am trying to zip a directory using python zipfile module and its working well.But now i want to exclude some folders.ie if my director tree is like

abc
def
ghi
jkl
mno

then i want to archive all to myfile.zip but excluding "ghi"

I am trying to zip files using

zf = zipfile.ZipFile("Application server.zip", "w")
for dirname, subdirs, files in os.walk("D:\\review docs"):
    zf.write(dirname)
    for filename in files:
        zf.write(os.path.join(dirname, filename))
zf.close()

so this is archiving everything under "D:\review docs" to "Application server.zip" but i want to exclude some directories from the zip. In fact i can use linux commands to do the same but i want to use zipfile module. Also if i pop exclude folder name from "dirname" list optained from os.walk,will that work? further Adding up a check before zipping like if "dirname"=="exlude folder" will also work i think but i want a neat solution of doing the same using the module.I read some where that zipfile module provides this functionality but didn't found any code example for the same.

like image 665
thinkingmonster Avatar asked Aug 03 '15 04:08

thinkingmonster


People also ask

How do I exclude a ZIP file?

Exclude Multiple Files/Directories from Zip Archive You can define -x multiple times in a single zip command to exclude multiple files and directories from zip archive.

How to zip all files in a directory in Python?

To zip all files in a directory, we need to traverse every file in the directory and zip one by one individually using the zipfile module. We can use the walk () function from the os module to traverse through the directory. Example of zipping all files in a directory in Python from zipfile import ZipFile

What is zipfile in Python?

Python’s zipfile is a standard library module intended to manipulate ZIP files. This file format is a widely adopted industry standard when it comes to archiving and compressing digital data. You can use it to package together several related files. It also allows you to reduce the size of your files and save disk space.

How can I create a zip file without using a zipfile?

A quick and high-level way to create a ZIP file without using zipfile is to use shutil. This module allows you to perform several high-level operations on files and collections of files. When it comes to archiving operations, you have make_archive (), which can create archives, such as ZIP or TAR files:

How to create a zip file without compression in Python?

ZIP files are used as a base file format by many programs and use .zip file extension. How to create a zip file with the zipfile module without compression? In Python, we can create zip files using the ZipFile () method of the zipfile module. We can then add other files to the zip file.


1 Answers

Yes , you can remove elements from the subdirs , that would make sure that os.walk() does not into those directories. Example -

for dirname, subdirs, files in os.walk("D:\\review docs"):
    if 'exclude directory' in subdirs:
        subdirs.remove('exclude directory')
    zf.write(dirname)
    for filename in files:
        zf.write(os.path.join(dirname, filename))
zf.close()
like image 131
Anand S Kumar Avatar answered Sep 22 '22 10:09

Anand S Kumar