The gzip Module

The gzip module allows you to read and write gzip-compressed files as if they were ordinary files, as shown in Example 5-24.

Example 5-24. Using the gzip Module to Read a Compressed File

File: gzip-example-1.py

import gzip

file = gzip.GzipFile("samples/sample.gz")

print file.read()

Well it certainly looks as though we're in for
a splendid afternoon's sport in this the 127th
Upperclass Twit of the Year Show.

The standard implementation doesn't support the seek and tell methods. Example 5-25 shows how to add forward seeking.

Example 5-25. Extending the gzip Module to Support seek/tell

File: gzip-example-2.py

import gzip

class gzipFile(gzip.GzipFile):
 # adds seek/tell support to GzipFile

 offset = 0

 def read(self, size=None):
 data = gzip.GzipFile.read(self, size)
 self.offset = self.offset + len(data)
 return data

 def seek(self, offset, whence=0):
 # figure out new position (we can only seek forwards)
 if whence == 0:
 position = offset
 elif whence == 1:
 position = self.offset + offset
 raise IOError, "Illegal argument"
 if position < self.offset:
 raise IOError, "Cannot seek backwards"

 # skip forward, in 16k blocks
 while position > self.offset:
 if not self.read(min(position - self.offset, 16384)):

 def tell(self):
 return self.offset

# try it

file = gzipFile("samples/sample.gz")

print file.read()

this the 127th
Upperclass Twit of the Year Show.

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols


Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net