gzip module - help!

Discussion in 'Python' started by bmgz, Dec 14, 2003.

  1. bmgz

    bmgz Guest

    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?
     
    bmgz, Dec 14, 2003
    #1
    1. Advertising

  2. bmgz

    bmgz Guest

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

    "bmgz" <> wrote in message news:...
    > 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?
    >
    >
    >
    >
     
    bmgz, Dec 14, 2003
    #2
    1. Advertising

  3. "bmgz" <> 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>
     
    Fredrik Lundh, Dec 14, 2003
    #3
  4. bmgz

    Peter Hansen Guest

    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
     
    Peter Hansen, Dec 15, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    10
    Views:
    725
  2. Xah Lee
    Replies:
    25
    Views:
    898
    Jeffrey Schwab
    Nov 9, 2005
  3. syahreza.octadian

    import gzip error (please help)

    syahreza.octadian, Jan 11, 2008, in forum: Python
    Replies:
    2
    Views:
    637
    syahreza.octadian
    Jan 14, 2008
  4. Byron Rios
    Replies:
    0
    Views:
    253
    Byron Rios
    Jun 22, 2008
  5. Xah Lee
    Replies:
    10
    Views:
    527
    Jeffrey Schwab
    Nov 9, 2005
Loading...

Share This Page