Python
  Home arrow Python arrow Python UnZipped
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python UnZipped
By: Mark Lee Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 51
    2004-01-08

    Table of Contents:
  • Python UnZipped
  • Going Full Monty with the Zip File
  • Listings in the Key of Zip

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Python UnZipped
    (Page 1 of 3 )

    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.


     

    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


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway