Thursday, January 17, 2013

File compression in python

Normally, you can do
 f = open(fname, 'r')  
 data = f.read()  
 f.close()  
to read a file, and
 f = open(fname, 'w')  
 f.write(text)  
 f.close()  
to write a file.

To use gzip compression, do
 import gzip  
 f = gzip.open(fname, 'rb')  
 data = f.read()  
 f.close()  
to read a gzip-compressed file, and
 f = gzip.open(fname, 'wb')  
 f.write(data)  
 f.close()  
to write a gzip-compressed file.

To compress pickled objects, just use the file handle returned by gzip.open(); everything else remains the same.

(Courtesy Doug Hellmann.)

No comments:

Post a Comment