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