gzip module - help!

B

bmgz

I am having problems trying to use the gzip module, I do the followig

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'


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?
 
F

Fredrik Lundh

bmgz said:
I am having problems trying to use the gzip module, I do the followig

this attempts to open a compressed file named "testfile.txt". is
this what you want?

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'
Traceback (most recent call last):
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>
 
P

Peter Hansen

bmgz said:
Sorry, I forgot to mention that i am trying to create an archive.

As I understand it, gzip is about compressing, while tar and zip are about
archives. You can't use gzip to make an archive, you can only use it
to compress or decompress... archives, or other files.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top