473,385 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 5320
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
8
by: Irmen de Jong | last post by:
What would be the best way, if any, to obtain the bytecode for a given loaded module? I can get the source: import inspect import os src = inspect.getsource(os) but there is no...
5
by: dody suria wijaya | last post by:
I found this problem when trying to split a module into two. Here's an example: ============== #Module a (a.py): from b import * class Main: pass ============== ==============
3
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
10
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
40
by: rjcarr | last post by:
Sorry if this is a completely newbie question ... I was trying to get information about the logging.handlers module, so I imported logging, and tried dir(logging.handlers), but got: ...
4
by: rkmr.em | last post by:
Hi I have a function data, that I need to import from a file data, in the directory data If I do this from python interactive shell (linux fedora core 8) from dir /home/mark it works fine: ...
0
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so...
6
by: dudeja.rajat | last post by:
Hi, I found on the net that there is something called module initialization. Unfortunately, there is not much information for this. However, small the information I found module initialization...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.