473,289 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 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 2640
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 unix admin and perl, i have quickly located the...
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 Python tutorial 8 months ago, have spent 30...
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 object is like so: if filename.endswith(".gz"):...
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 kernel module. Please suggest me easiest way to...
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 increase came from the images. The total size of...
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, Sep 20 2006, 03:46:40) on sunos5 Type "help",...
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 software packages... The situation is that I'm trying...
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) a=numpy.zeros(1000000,dtype=numpy.uint8) fd =...
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 = open(gunziped_file,'r') But I get the nezt...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.