bz2 module

B

Brad Tilley

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
 
A

Alex Martelli

Brad Tilley said:
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...:
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
 
B

Brad Tilley

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




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()




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
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Brad said:
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
 
J

Jeff Epler

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)

iD8DBQFBdF30Jd01MZaTXX0RAqKWAKCaZgSCBq6Ob7BT6HO33oUhHB+vZQCfSLAy
xn3XnyrgyjJQdAmjmCPlnk0=
=MRlx
-----END PGP SIGNATURE-----
 
B

Brad Tilley

Jeff said:
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!
 
A

Alex Martelli

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


"You're welcome", by the way.

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
 
R

Raymond Hettinger

[Alex Martelli]
. . .
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 ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top