gzip module - help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bmgz

    gzip module - help!

    I am having problems trying to use the gzip module, I do the followig
    [color=blue][color=green][color=darkred]
    >>>import gzip
    >>>file = gzip.GzipFile(" testfile.txt")
    >>>file.write () -which params does this accept?, archive name?[/color][/color][/color]

    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.t xt", '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

    #2
    Re: gzip module - help!

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

    "bmgz" <bmgz@dev.nul l> wrote in message news:3fdc2a06.0 @news1.mweb.co. za...[color=blue]
    > I am having problems trying to use the gzip module, I do the followig
    >[color=green][color=darkred]
    > >>>import gzip
    > >>>file = gzip.GzipFile(" testfile.txt")
    > >>>file.write () -which params does this accept?, archive name?[/color][/color]
    >
    > 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.t xt", '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?
    >
    >
    >
    >[/color]


    Comment

    • Fredrik Lundh

      #3
      Re: gzip module - help!

      "bmgz" <bmgz@dev.nul l> wrote:
      [color=blue]
      > I am having problems trying to use the gzip module, I do the followig
      >[color=green][color=darkred]
      > >>>import gzip
      > >>>file = gzip.GzipFile(" testfile.txt")[/color][/color][/color]

      this attempts to open a compressed file named "testfile.t xt". is
      this what you want?
      [color=blue][color=green][color=darkred]
      > >>>file.write () -which params does this accept?, archive name?[/color][/color][/color]

      the data you want to store in the file. GzipFile returns a file object,
      just like an ordinary open.
      [color=blue]
      > 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'[/color]

      on my machine, that call gives this error:
      [color=blue][color=green][color=darkred]
      >>> f.write()[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: write() takes exactly 2 arguments (1 given)
      [color=blue]
      > But if I include a mode in "gzip.GzipFile( "testfile.t xt", '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?[/color]

      on my machine, that creates a compressed file named "testfile.t xt",
      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.copyfile obj(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>




      Comment

      • Peter Hansen

        #4
        Re: gzip module - help!

        bmgz wrote:[color=blue]
        >
        > Sorry, I forgot to mention that i am trying to create an archive.[/color]

        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

        Comment

        Working...