472,353 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 2551
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Xah Lee | last post by:
today i need to use Python to decompress gzip files. since i'm familiar with Python doc and have 10 years of computing experience with 4 years in...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official...
14
by: Bill | last post by:
I've written a small program that, in part, reads in a file and parses it. Sometimes, the file is gzipped. The code that I use to get the file...
0
by: sarang | last post by:
Hi, I want to use gzip compression library in kernel module. I have source of of gzip library. I dont understand how to use this library in...
4
by: Showjumper | last post by:
I tried using a custom Http Module to Gzip pages since my host wont enable GZIP via IIS. What i found was that page size actually increased - the...
2
by: syahreza.octadian | last post by:
Dear all, Please help, i have error message when i import gzip module. The error like this below: bash-3.00$ python Python 2.5 (r25:51908,...
4
by: Aidan | last post by:
Hi, I'm having a bit of trouble with a python script I wrote, though I'm not sure if it's related directly to python, or one of the other...
3
by: Sean Davis | last post by:
I have a set of numpy arrays which I would like to save to a gzip file. Here is an example without gzip: b=numpy.ones(1000000,dtype=numpy.uint8)...
1
by: Nader | last post by:
Hello, I have a gzip file and I try to read from this file withe the next statements: gunziped_file = gzip.GzipFile('gzip-file') input_file =...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.