Create .zip in Python?

The zipfile.write() method takes an optional arcname argument
that specifies what the name of the file should be inside the zipfile.

You can use this to strip off the path to src at the beginning. Here I
use os.path.abspath() to make sure that both src and the
filename returned by os.walk() have a common prefix.

#!/usr/bin/env python2.7

import os
import zipfile

def zip(src, dst):
    zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(src)
    for dirname, subdirs, files in os.walk(src):
        for filename in files:
            absname = os.path.abspath(os.path.join(dirname, filename))
            arcname = absname[len(abs_src) + 1:]
            print 'zipping %s as %s' % (os.path.join(dirname, filename),
                                        arcname)
            zf.write(absname, arcname)
    zf.close()

zip("src", "dst")

With a directory structure like this:

src
└── a
    ├── b
    │   └── bar
    └── foo

The script prints:

zipping src/a/foo as a/foo
zipping src/a/b/bar as a/b/bar

And the contents of the resulting zip file are:

Archive:  dst.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  01-28-13 11:36   a/foo
        0  01-28-13 11:36   a/b/bar
 --------                   -------
        0                   2 files

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)