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.)
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?
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:
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.
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:
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!"
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).
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).
blog comments powered by Disqus |
|
|
|
|
|
|
|