Create .zip in Python?

爷,独闯天下 提交于 2019-12-02 18:50:10

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

From what I can tell you are close. You could use dirname and basename to make sure you are grabbing the right path name:

>>> os.path.dirname("/path/to/dst")
'/path/to'
>>> os.path.basename("/path/to/dst")
'dst'

Then using chdir you can make sure you are in the parent so the paths are relative.

def zip(src, dst):
    parent = os.path.dirname(dst)
    folder = os.path.basename(dst)

    os.chdir(parent):
    for dirname, subdirs, filenames in os.walk(folder):
        ...

This creates:

dst/a.txt
dst/b
dst/b/c.txt
...etc...

If do not want to include the name "dst" you can just do os.chdir(dst) and then os.walk('.').

Hope that helps.

Use the arcname parameter to control the name/path in the zip file.

For example, for a zip file that contains only files, no directories:

zf.write(os.path.join(dirname, filename), arcname=filename)

Or to invent a new directory inside the zip file:

zf.write(os.path.join(dirname, filename), arcname=os.path.join("my_zip_dir", filename))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!