Python
  Home arrow Python arrow Page 3 - 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? 
Google.com  
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 - Listings in the Key of Zip
    ( Page 3 of 3 )

    Finally were going to look at a function that uses recursion to move though a Zip file and its sub archives; returning a complete list of all none Zip files. But what’s the point in this? Let's say for instance that you want to count the number of files in a Zip, this way all you have to do is call len() on our function. Enough talk lets see this function in action.


    <br />#!/usr/bin/env python</p>
    <p> </p>
    <
    p>import oszipfile</p>
    <
    p> </p>
    <
    p>def rezipe(pathfiles = []):</p>
    <
    p> </p>
    <
    p>zip zipfile.ZipFile(path)</p>
    <
    p> </p>
    <
    p>for name in zip.namelist():</p>
    <
    p>if name.endswith('.zip'):</p>
    <
    p>file(name'wb').write(zip.read(name))</p>
    <
    p>rezipe(name)</p>
    <
    p>os.remove(name)</p>
    <
    p>elif not name.endswith('/'):</p>
    <
    p>files.append(name)</p>
    <
    p> </p>
    <
    p>return files</p>
    <
    p> </p>
    <
    p>print len(rezipe('Python.zip'))
    <
    br />

    But wait, there's... uhm... nevermind. That's it. Sorry if I hyped that last one up a bit.

    Unlike our other examples rezipe() opens the Zip file itself instead of using one we’ve already opened. It then loops though zip.namelist() and if name ends with .zip we extract it to the current working directory and call rezipe() on it, removing it when rezipe() is complete. The next part simply says if name isn’t a Zip file or a folder append it to the end of our list.

    If your anything like me by now I’m sure you can see the potential this little guy (in particular) has and what this means for your Zip files!

    If you’ve found this article interesting and you want to learn more about Python or some of the subjects covered here:

    http://www.python.org/ - Python homepage
    http://www.python.org/doc/2.3.2/tut/tut.html - Python tutorial
    http://www.python.org/doc/2.3.2/modindex.html - Python module index
    http://www.python.org/doc/2.3.2/lib/module-zipfile.html - The zipfile module

    Note: All the examples shown and discussed in this article where tested on Windows XP with Python 2.3 and are meant only as examples.



     
     
    >>> 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
    For more Enterprise Application Development news, visit eWeek