Python
  Home arrow Python arrow Page 2 - 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Python UnZipped - Going Full Monty with the Zip File
    (Page 2 of 3 )

    You guessed it, where going to unzip them. (Using our file.txt and file.gif sample files again just to make things easier to follow.)


    <br />>>> import zipfile
    <br />>>> zip zipfile.ZipFile('Python.zip''r')
    <
    br />>>> file(' 'file.txt', 'w').write(zip.read('file.txt'))
    <br />>>> file('
    file.gif', 'wb').write(zip.read('file.gif'))
    <br />>>> zip.close()
    <br />

    Note: Images are binary; I’ve used the 'wb' (write binary) flag for the second file although this may not always be necessary.

    Ok we just extracted two files from our Zip, and in only five lines! And this example is fine if you know the names of the files you want to extract, but what if you don't?


    <br />#!/usr/bin/env python</p>
    <p> </p>
    <
    p>import zipfile</p>
    <
    p> </p>
    <
    p>def inzip(filenamezip):</p>
    <
    p>return filename in zip.namelist()</p>
    <
    p> </p>
    <
    p>if __name__ == '__main__':</p>
    <
    p> </p>
    <
    p>zip zipfile.ZipFile('Python.zip''r')</p>
    <
    p>inzip('file.txt'zip)</p>
    <
    p>zip.close()
    <
    br />

    Short and sweet just like its name, this function simply returns True or False (True in the example above) if the string filename is in the list of files in the zip.

    The namelist() method (along with its brothers and sisters) provides information about a Zip file, namelist() itself returns a list of all the files within a Zip. For example:


    <br />>>> zip.namelist()</p>
    <
    p>['1.txt''2.txt''3.txt''file.gif''file.txt''folder/file.html'‘folder/]
    <
    br />>>>
    <
    br />

    You’ve checked the contents of the file and you want to get extracting. Rather than sitting there typing names into your Python shell one by one (which lets face it is pretty boring), I’m going to show you how.


    <br />#!/usr/bin/env python </p>
    <p> </p>
    <
    p>import zipfile </p>
    <
    p> </p>
    <
    p>def unzip(zip): </p>
    <
    p>for name in zip.namelist(): </p>
    <
    p>file(name'wb').write(zip.read(name)) </p>
    <
    p> </p>
    <
    p>if __name__ == '__main__': </p>
    <
    p> </p>
    <
    p>zip zipfile.ZipFile('Python.zip''r') </p>
    <
    p>unzip(zip) </p>
    <
    p>zip.close()
    <
    br />

    This is fine for a flat Zip files (those without subfolders) but it’d just barf all over the screen if we passed a name that included a none existent directory to file(), there are two choices:

    1. Remove everything before the filename, simple yes, but you could end up two files named the same and we all know what happens next.
    2. We can create all the directories before unzipping our file, which is a lot safer, though requires a little more work…

    Of course we’re going for the second choice, not only is it the most interesting but also the most Pythonic!

    To borrow from another TV snake (Black Adder) "I have a cunning plan!"


    <br />#!/usr/bin/env python</p>
    <p> </p>
    <
    p>import oszipfile</p>
    <
    p> </p>
    <
    p>def unzip(pathzip):</p>
    <
    p> </p>
    <
    p>isdir os.path.isdir
    <br />join os.path.join
    <br />norm os.path.normpath
    <br />split os.path.split</p>
    <
    p> </p>
    <
    p>for each in zip.namelist():</p>
    <
    p>if not each.endswith('/'):</p>
    <
    p>rootname split(each)</p>
    <
    p>directory norm(join(pathroot))</p>
    <
    p>if not isdir(directory):</p>
    <
    p>os.makedirs(directory)</p>
    <
    p>file(join(directoryname), 'wb').write(zip.read(each))</p>
    <
    p> </p>
    <
    p>if __name__ == '__main__':</p>
    <
    p> </p>
    <
    p>zip zipfile.ZipFile('Python.zip''r')</p>
    <
    p>unzip(''zip)</p>
    <
    p>zip.close()
    <
    br />

    Don’t panic! This is a little more advanced than the other functions we've created so far and there’s actually quite a lot going on inside it so we'll go though step by step; you might have noticed the os module sitting at the core of this example too.

    The first part of this function is pretty strange as functions go; basically all it does is create some local copies of some of the functions from os.path (to improve performance).

    Next we loop though each of the names in zip.namelist() and if the name isn’t a directory (end with a forward slash).


    <br />>>> for each in zip.namelist(): print each
    <br />1.txt
    <br />2.txt
    <br />3.txt
    <br />file.gif
    <br />file.txt
    <br />folder/file.html
    <br />folder/
    <
    br />

    The path is split from the filename and assigned to root, name. Our next line creates a variable named directory that holds the new path for the file, which is simply path and root joined.

    Note: This won't work with absolute paths like C:FolderFolderFile.ext; in this case the file should be extracted to that location (tested on windows). For this example I'm assuming that absolute paths won’t be used.

    All we do then is check if the directory tree does NOT already exist before attempting to create it and extracting our file. Overall, it's a very small function (especially compared to some other languages).

    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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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