Help with python-list archives

Ian Kelly ian.g.kelly at gmail.com
Fri Jan 6 02:41:01 EST 2012


On Thu, Jan 5, 2012 at 8:00 PM, MRAB <python at mrabarnett.plus.com> wrote:
> import gzip
>
> in_file = gzip.open(r"C:\2012-January.txt.gz")
> out_file = open(r"C:\2012-January.txt.tmp", "wb")
> out_file.write(in_file.read())
> in_file.close()
> out_file.close()
>
> in_file = gzip.open(r"C:\2012-January.txt.tmp")
> out_file = open(r"C:\2012-January.txt", "wb")
> out_file.write(in_file.read())
> in_file.close()
> out_file.close()

One could also avoid creating the intermediate file by using a
StringIO to keep it in memory instead:

import gzip
from cStringIO import StringIO

in_file = gzip.open('2012-January.txt.gz')
tmp_file = StringIO(in_file.read())
in_file.close()
in_file = gzip.GzipFile(fileobj=tmp_file)
out_file = open('2012-January.txt', 'wb')
out_file.write(in_file.read())
in_file.close()
out_file.close()

Sadly, GzipFile won't read directly from another GzipFile instance
(ValueError: Seek from end not supported), so some sort of
intermediate is necessary.



More information about the Python-list mailing list