Compress a directory tree in Python with zipfile

Trying to compress a folder with the Python module zipfile results in an IOError exception being thrown. To overcome this simply combine os.walk with arcname argument of zipfile.write:


Z = zipfile.ZipFile('teste.zip', 'w')
for r, d, f in os.walk('teste'):
  for ff in f:
    Z.write(os.path.join(r, ff), os.path.join(r, ff))
Z.close()

One thought on “Compress a directory tree in Python with zipfile

  1. Pingback: Compressing Files with Python: Symlink Trouble! | Doei Doei

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s