gzip module - help!
Fredrik Lundh
fredrik at pythonware.com
Sun Dec 14 04:31:50 EST 2003
"bmgz" <bmgz at dev.null> wrote:
> I am having problems trying to use the gzip module, I do the followig
>
> >>>import gzip
> >>>file = gzip.GzipFile("testfile.txt")
this attempts to open a compressed file named "testfile.txt". is
this what you want?
> >>>file.write() -which params does this accept?, archive name?
the data you want to store in the file. GzipFile returns a file object,
just like an ordinary open.
> I get this ERROR:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "/usr/lib/python2.2/gzip.py", line 139, in write
> self.size = self.size + len(data)
> AttributeError: GzipFile instance has no attribute 'size'
on my machine, that call gives this error:
>>> f.write()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: write() takes exactly 2 arguments (1 given)
> But if I include a mode in "gzip.GzipFile("testfile.txt", 'wb')" or
> something like that I don't get an error
> and then I manage to do >>>file.close() but still I can't find any
> compressed file?
on my machine, that creates a compressed file named "testfile.txt",
which unzips to nothing.
maybe this is what you want:
import gzip, shutil
infile = open("testfile.txt") # text file to compress
outfile = gzip.GzipFile("testfile.txt.gz", "wb") # archive file
shutil.copyfileobj(infile, outfile)
outfile.close()
to compress a binary file, make sure you pass "rb" as the second
argument to the first open:
infile = open("testfile.dat", "rb") # binary file to compress
(for details, read the gzip and shutil chapters in the library reference)
</F>
More information about the Python-list
mailing list