469,336 Members | 5,276 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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('test.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

Jul 18 '05 #1
7 5080
Brad Tilley <br********@gmail.com> wrote:
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('test.bkf.copy', 'w').write(data)

x.close()

It fails with this error:
AttributeError: 'module' object has no attribute 'BZ2File'
Looks like your Python is misinstalled, or something...:
import bz2
bz2.BZ2File <type 'bz2.BZ2File'>

Can anyone give me an example of how to use bz2 to compress files. Why
import bz2
source = open('test.bkf', 'rb')
destination = bz2.BZ2File('test.bkf.bz2', 'w')

while True:
data = source.read(1024000)
if not data: break
destination.write(data)

destination.close()
source.close()
don't the docs give examples of this?!?


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
Jul 18 '05 #2
Alex Martelli wrote:
Brad Tilley <br********@gmail.com> wrote:

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('test.bkf.copy', 'w').write(data)

x.close()

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

Looks like your Python is misinstalled, or something...:

import bz2
bz2.BZ2File


<type 'bz2.BZ2File'>

Can anyone give me an example of how to use bz2 to compress files. Why

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

while True:
data = source.read(1024000)
if not data: break
destination.write(data)

destination.close()
source.close()

don't the docs give examples of this?!?

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


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
Jul 18 '05 #3
Brad Tilley wrote:
It fails with this error:
AttributeError: 'module' object has no attribute 'BZ2File'


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
Jul 18 '05 #4
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)

iD8DBQFBdF30Jd01MZaTXX0RAqKWAKCaZgSCBq6Ob7BT6HO33o UhHB+vZQCfSLAy
xn3XnyrgyjJQdAmjmCPlnk0=
=MRlx
-----END PGP SIGNATURE-----

Jul 18 '05 #5
Jeff Epler wrote:
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


That is it exactly. thank you!
Jul 18 '05 #6
Brad Tilley <br********@gmail.com> wrote:
...
AttributeError: 'module' object has no attribute 'BZ2File'


Looks like your Python is misinstalled, or something...:
>import bz2
>bz2.BZ2File


<type 'bz2.BZ2File'>
....so this is the first thing you should investigate...

Can anyone give me an example of how to use bz2 to compress files. Why


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

while True:
data = source.read(1024000)
if not data: break
destination.write(data)

destination.close()
source.close()
"You're welcome", by the way.

don't the docs give examples of this?!?


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...


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.


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
Jul 18 '05 #7
[Alex Martelli]
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...
. . . 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.


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 ;-)
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Irmen de Jong | last post: by
5 posts views Thread by dody suria wijaya | last post: by
3 posts views Thread by David T. Ashley | last post: by
40 posts views Thread by rjcarr | last post: by
4 posts views Thread by rkmr.em | last post: by
reply views Thread by Fredrik Lundh | last post: by
6 posts views Thread by dudeja.rajat | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.