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

 
Developer Updates  
Free Website Content 
 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: starstarstarstarstar / 53
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Python UnZipped
    ( Page 1 of 3 )

    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.


     

    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

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT