O'Reilly logo

Python 2.6 Text Processing Beginner's Guide by Jeff McNeil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - handling compressed files

In this example, we'll add support for common compression formats using Python's standard library.

  1. Using the code in logscan-c.py as your starting point, create logscan-d.py. Add a new function just below the MaxSizeHandler class.
    def get_stream(path):
    	"""
    	Detect compression.
    	If the file name ends in a compression
    	suffix, we'll open it using the correct
    	algorith. If not, we just return a standard
    	file object.
    	"""
    	_open = open
    	if path.endswith('.gz'):
    		_open = gzip.open
    	elif path.endswith('.bz2'):
    		_open = bz2.open
    	return _open(path)
    
  2. Within our main section, update the line that reads open(opts.file) to read get_stream(opts.file)..
  3. At the top of the listing, ensure that you're importing the two new compression ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required