Home arrow Python arrow Page 3 - Python UnZipped

Listings in the Key of Zip - Python

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.

  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



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>elif not name.endswith('/'):</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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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