bz2 module

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

    #1

    bz2 module

    I'm having a bit of trouble using the bz2 module. The documentation for
    it is very poor. Here's what I'm trying to do:

    import bz2

    x = file('test.bkf' , 'rb')

    while True:
    data = x.read(1024000)
    if not data:
    break
    bz2.BZ2File('te st.bkf.copy', 'w').write(data )

    x.close()

    It fails with this error:
    AttributeError: 'module' object has no attribute 'BZ2File'

    Can anyone give me an example of how to use bz2 to compress files. Why
    don't the docs give examples of this?!?

    Thanks,
    Brad



  • Alex Martelli

    #2
    Re: bz2 module

    Brad Tilley <bradtilley@gma il.com> wrote:
    [color=blue]
    > I'm having a bit of trouble using the bz2 module. The documentation for
    > it is very poor. Here's what I'm trying to do:
    >
    > import bz2
    >
    > x = file('test.bkf' , 'rb')
    >
    > while True:
    > data = x.read(1024000)
    > if not data:
    > break
    > bz2.BZ2File('te st.bkf.copy', 'w').write(data )
    >
    > x.close()
    >
    > It fails with this error:
    > AttributeError: 'module' object has no attribute 'BZ2File'[/color]

    Looks like your Python is misinstalled, or something...:
    [color=blue][color=green][color=darkred]
    >>> import bz2
    >>> bz2.BZ2File[/color][/color][/color]
    <type 'bz2.BZ2File'>[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    [color=blue]
    > Can anyone give me an example of how to use bz2 to compress files. Why[/color]

    import bz2
    source = open('test.bkf' , 'rb')
    destination = bz2.BZ2File('te st.bkf.bz2', 'w')

    while True:
    data = source.read(102 4000)
    if not data: break
    destination.wri te(data)

    destination.clo se()
    source.close()
    [color=blue]
    > don't the docs give examples of this?!?[/color]

    If the docs were perfect, who'd ever buy Python in a Nutshell, or the
    Python Cookbook? So I sneak in at night in the CVS repository and
    secretly sabotage them just enough, destroying, without leaving a trace,
    all the wonderful doc patches that people are submitting all the time,
    fixing problems rather than whining about them...


    Alex

    Comment

    • Brad Tilley

      #3
      Re: bz2 module

      Alex Martelli wrote:[color=blue]
      > Brad Tilley <bradtilley@gma il.com> wrote:
      >
      >[color=green]
      >>I'm having a bit of trouble using the bz2 module. The documentation for
      >>it is very poor. Here's what I'm trying to do:
      >>
      >>import bz2
      >>
      >>x = file('test.bkf' , 'rb')
      >>
      >>while True:
      >> data = x.read(1024000)
      >> if not data:
      >> break
      >> bz2.BZ2File('te st.bkf.copy', 'w').write(data )
      >>
      >>x.close()
      >>
      >>It fails with this error:
      >>AttributeErro r: 'module' object has no attribute 'BZ2File'[/color]
      >
      >
      > Looks like your Python is misinstalled, or something...:
      >
      >[color=green][color=darkred]
      >>>>import bz2
      >>>>bz2.BZ2Fi le[/color][/color]
      >
      > <type 'bz2.BZ2File'>
      >
      >[color=green]
      >>Can anyone give me an example of how to use bz2 to compress files. Why[/color]
      >
      >
      > import bz2
      > source = open('test.bkf' , 'rb')
      > destination = bz2.BZ2File('te st.bkf.bz2', 'w')
      >
      > while True:
      > data = source.read(102 4000)
      > if not data: break
      > destination.wri te(data)
      >
      > destination.clo se()
      > source.close()
      >
      >[color=green]
      >>don't the docs give examples of this?!?[/color]
      >
      >
      > If the docs were perfect, who'd ever buy Python in a Nutshell, or the
      > Python Cookbook? So I sneak in at night in the CVS repository and
      > secretly sabotage them just enough, destroying, without leaving a trace,
      > all the wonderful doc patches that people are submitting all the time,
      > fixing problems rather than whining about them...
      >
      >
      > Alex[/color]

      I don't know enough about proper Python usage to submit doc patches, so
      when I google and can't find an answer and none of my coworkers can
      answer my question, I post here for help... I wouldn't call that
      whining, but you're entitled to your opinion of my post.

      If I were knowledgeable enough to submit doc pathces, I would do so.

      Thank you,

      Brad

      Comment

      • Martin v. Löwis

        #4
        Re: bz2 module

        Brad Tilley wrote:[color=blue]
        > It fails with this error:
        > AttributeError: 'module' object has no attribute 'BZ2File'[/color]

        Can you please print bz2.__file__ right before the error?
        On my system, it gives

        /usr/lib/python2.3/lib-dynload/bz2.so

        I wonder what bz2 possibly could be on your system if importing
        it succeeds, but looking for BZ2file fails.

        Regards,
        Martin

        Comment

        • Jeff Epler

          #5
          Re: bz2 module

          Is it possible that you've fallen for the common pitfall
          "I named my test program xxx.py, and module xxx doesn't work"?

          If your program is called bz2.py, then rename it, and remove bz2.pyc.
          Now things may work better.

          Jeff

          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.2.6 (GNU/Linux)

          iD8DBQFBdF30Jd0 1MZaTXX0RAqKWAK CaZgSCBq6Ob7BT6 HO33oUhHB+vZQCf SLAy
          xn3XnyrgyjJQdAm jmCPlnk0=
          =MRlx
          -----END PGP SIGNATURE-----

          Comment

          • Brad Tilley

            #6
            Re: bz2 module

            Jeff Epler wrote:[color=blue]
            > Is it possible that you've fallen for the common pitfall
            > "I named my test program xxx.py, and module xxx doesn't work"?
            >
            > If your program is called bz2.py, then rename it, and remove bz2.pyc.
            > Now things may work better.
            >
            > Jeff[/color]

            That is it exactly. thank you!

            Comment

            • Alex Martelli

              #7
              Re: bz2 module

              Brad Tilley <bradtilley@gma il.com> wrote:
              ...[color=blue][color=green][color=darkred]
              > >>AttributeErro r: 'module' object has no attribute 'BZ2File'[/color]
              > >
              > > Looks like your Python is misinstalled, or something...:
              > >[color=darkred]
              > >>>>import bz2
              > >>>>bz2.BZ2Fi le[/color]
              > >
              > > <type 'bz2.BZ2File'>[/color][/color]

              ....so this is the first thing you should investigate...

              [color=blue][color=green][color=darkred]
              > >>Can anyone give me an example of how to use bz2 to compress files. Why[/color]
              > >
              > > import bz2
              > > source = open('test.bkf' , 'rb')
              > > destination = bz2.BZ2File('te st.bkf.bz2', 'w')
              > >
              > > while True:
              > > data = source.read(102 4000)
              > > if not data: break
              > > destination.wri te(data)
              > >
              > > destination.clo se()
              > > source.close()[/color][/color]

              "You're welcome", by the way.

              [color=blue][color=green][color=darkred]
              > >>don't the docs give examples of this?!?[/color]
              > >
              > > If the docs were perfect, who'd ever buy Python in a Nutshell, or the
              > > Python Cookbook? So I sneak in at night in the CVS repository and
              > > secretly sabotage them just enough, destroying, without leaving a trace,
              > > all the wonderful doc patches that people are submitting all the time,
              > > fixing problems rather than whining about them...[/color]
              >
              > I don't know enough about proper Python usage to submit doc patches, so
              > when I google and can't find an answer and none of my coworkers can
              > answer my question, I post here for help... I wouldn't call that
              > whining, but you're entitled to your opinion of my post.[/color]

              Touchy, aren't we? Bet you took the part about sneaking in at night &c
              at literal value too -- after all, there was no smiley, so I clearly
              must be in deadly earnest rather than deadpanning a friendly ribbing.

              The answer, turned back to serious mood, means: if something is missing
              in the docs it's because people who think it should be there have not
              submitted a doc patch for it. I'm not comfortable with the library
              *reference* being full of examples; I think it should probably be split
              into a reference (a real one) and a collection of how-to/tutorials (with
              the examples). So, I've submitted doc pathes for correction of errors,
              omissions, imperfect or ambiguous phrasing, etc, but not ones for the
              addition of examples.

              The most _systematic_ collection of examples of use of the standard
              library is neither of my books, btw, but rather Lundh's "Standard Python
              Library" book -- it's a bit dated, but it has examples of use of _every_
              module that was in the library when the book was written.


              Alex

              Comment

              • Raymond Hettinger

                #8
                Re: bz2 module

                [Alex Martelli][color=blue][color=green][color=darkred]
                > > > If the docs were perfect, who'd ever buy Python in a Nutshell, or the
                > > > Python Cookbook? So I sneak in at night in the CVS repository and
                > > > secretly sabotage them just enough, destroying, without leaving a trace,
                > > > all the wonderful doc patches that people are submitting all the time,
                > > > fixing problems rather than whining about them...[/color][/color][/color]
                . . .[color=blue]
                > I'm not comfortable with the library
                > *reference* being full of examples; I think it should probably be split
                > into a reference (a real one) and a collection of how-to/tutorials (with
                > the examples). So, I've submitted doc pathes for correction of errors,
                > omissions, imperfect or ambiguous phrasing, etc, but not ones for the
                > addition of examples.[/color]

                As a countermeaure for Alex secretly sabotaging doc patches, I've been
                waging a counter campaign to put *more* examples in the reference ;-)

                While Alex wasn't looking, I added the "Basic Example" for the
                unittest docs. Now, it's possible to get some immediate benefit from
                the module without reading the other seven sections or buying Alex's
                wonderful books.

                The new decimal module has a "Quick Start" section, the itertools
                module has both examples and recipes, and now even the tutorial has
                two new chapters chuck full of examples covering a swath of the
                Standard Library.

                Still more valiant efforts are needed. Currently, only his book
                effectively documents the __new__ method and shows how to use
                meta-classes. When his guard is down, *someone* should sneak in a doc
                patch. Unless we all contribute, Alex's superb books will continue to
                be must haves.


                Raymond


                P.S. Remember, this Alex character is dastardly. You may have to
                resort to slippery measures. For goodness sake, don't let him find
                out about all the superb examples carefully hidden in
                Lib/test/test_generators .py ;-)

                Comment

                Working...