Home arrow Python arrow Python UnZipped

Python UnZipped

pythonPython is a great choice for anyone wanting to play with the increasingly popular ZIP or GZIP (not covered here) file formats, and as usual Python makes it surprisingly fun/easy! Don't believe me? In this article we'll look at creating, extracting, and adding to Zip archives using Pythons standard zipfile module and defining a set of functions you can use with your own Zip files; ending with an example which recursively scans a Zip file and sub-archives.

TABLE OF CONTENTS:
  1. Python UnZipped
  2. Going Full Monty with the Zip File
  3. Listings in the Key of Zip
By: Mark Lee Smith
Rating: starstarstarstarstar / 53
January 08, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement


 

Introduction

Python is a great choice for anyone wanting to play with the increasingly popular ZIP or GZIP (not covered here) file formats, and as usual Python makes it surprisingly fun/easy!

Don't believe me?

In this article we'll look at creating, extracting, and adding to Zip archives using Pythons standard zipfile module and defining a set of functions you can use with your own Zip files; ending with an example which recursively scans a Zip file and sub-archives.

This does require some prior knowledge of Python, so if you have never used Python before you should read Vikram Vaswani’s Python 101 before reading this.

Creating Our Zip File

Lets jump right in and create our Zip file, then add a few sample files to it.


<br />>>> import zipfile
<br />>>> zip zipfile.ZipFile('Python.zip''w')
<
br />>>> zip.write('file1.txt')
<
br />>>> zip.write('file2.gif')
<
br />>>> zip.close()
<
br />

So we should have small Zip containing two files (file.txt and file.gif) sitting in our current working directory. Easy enough and pretty neat overall. How about something a little more interesting? Adding all the .txt files in a directory to our archive, perhaps?


<br />#!/usr/bin/env python</p>
<p>import oszipfile</p>
<
p> </p>
<
p>def zipdir(pathextensionzip):</p>
<
p>for each in os.listdir(path):</p>
<
p>if each.endswith('.txt'):</p>
<
p>try: zip.write(path each)</p>
<
p>except IOErrorNone</p>
<
p>if __name__ == '__main__':</p>
<
p> </p>
<
p>zip zipfile.ZipFile('Python.zip''w')</p>
<
p>zipdir('''.txt'zip)</p>
<
p>zip.close()
<
br />

Still pretty simple. This example basically defines a new user function named zipdir(), which follows three steps..

  1. Loop though a list of all the names in our directory.
  2. If each ends with .txt try and write it to zip.
  3. If an IOError is raised skip this name and move onto the next one (this could happen if you have a folder ending with .txt)

There is a problem with this one though… because ZipFile is a file-based object, data already in our Zip gets wiped when we start writing again just like with normal files. Luckily this also means we can use other flags beside write, to show this we’ll add a few more files to our Zip using append.


<br />>>> import zipfile
<br />>>> zip zipfile.ZipFile('Python.zip''a')
<
br />>>> zip.write('file.txt')
<
br />>>> zip.write('file.gif')
<
br />>>> zip.write('folder/file.html')
<
br />>>> zip.close()
<
br />

So we’ve seen how to create a Zip file and we’ve added a set of files to it using write and append flags, what's next?



 
 
>>> More Python Articles          >>> More By Mark Lee Smith
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: