4.11 Reading Data from ZIP Files
Credit: Paul Prescod
4.11.1 Problem
You have an archive in ZIP
format, and you want to examine some or all of the files it contains
directly, without expanding them on disk.
4.11.2 Solution
ZIP files are a popular, cross-platform way of archiving files.
Python's standard library comes with a
zipfile
module to access
them easily:
import zipfile
z = zipfile.ZipFile("zipfile.zip", "r")
for filename in z.namelist( ):
print 'File:', filename,
bytes = z.read(filename)
print 'has',len(bytes),'bytes'
4.11.3 Discussion
Python can work directly with data in ZIP files. You can look at the
list of items in the directory and work with the data files
themselves. This recipe is a snippet that lists all of the names and
content lengths of the files included in the ZIP archive
zipfile.zip.
The zipfile module does not currently handle
multidisk ZIP files or ZIP files that have appended comments. Take
care to use 'r' as the flag argument, not
'rb', which might seem more natural (e.g., on
Windows). With ZipFile, the flag is not used the
same way as for opening a file, and 'rb' is not
recognized. The 'r' flag takes care of the
inherently binary nature of the ZIP file on all platforms.
4.11.4 See Also
Documentation for the zipfile module in the
Library Reference.
|