469,290 Members | 1,898 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

gzip module - help!

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
3 2384
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
"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
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.

Similar topics

10 posts views Thread by Xah Lee | last post: by
14 posts views Thread by Bill | last post: by
4 posts views Thread by Showjumper | last post: by
2 posts views Thread by syahreza.octadian | last post: by
4 posts views Thread by Aidan | last post: by
3 posts views Thread by Sean Davis | last post: by
1 post views Thread by Nader | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.