By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

gzip module - help!

P: n/a
I am having problems trying to use the gzip module, I do the followig
import gzip
file = gzip.GzipFile("testfile.txt")
file.write() -which params does this accept?, archive name?


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?


Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Sorry, I forgot to mention that i am trying to create an archive.

"bmgz" <bm**@dev.null> wrote in message news:3f********@news1.mweb.co.za...
I am having problems trying to use the gzip module, I do the followig
import gzip
file = gzip.GzipFile("testfile.txt")
file.write() -which params does this accept?, archive name?


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?

Jul 18 '05 #2

P: n/a
"bmgz" <bm**@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>


Jul 18 '05 #3

P: n/a
bmgz wrote:

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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.