import bz2 compress = bz2.compress('test.txt',9) f = open ("test.bz2", 'w') f.write(compress) f.close() print 'done.'