Does array.read() move file pointer automatically?

Lionel lionel.keene at gmail.com
Thu Feb 5 00:37:11 CET 2009


On Feb 4, 3:10 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
> Lionel wrote:
>
>  > Hello everyone. Quick question: When using the "read()" method in the
>  > array module, must I redirect the current file pointer or will that
>  > occur automatically?
>  >
>  > For example, if I were to sequentially read data in chunks from a
>  > binary file as in:
>  >
>  >
>  > for currentChunk in range(numberOfChunksToRead):
>  >
>  >        floatData = array.array('f')
>  >        floatData.read(MyFileHandle, numberOfFloatsPerChunk)
>  >        ...go to work on data...
>  >
>  >
>  > at each iteration of the "for" loop, will the next chunk of bytes be
>  > read into "floatData" or must I move the file pointer by calling "seek
>  > ()" or some function like that?
>  >
> The read() method has been deprecated since version Python 1.5.1. Use
> the fromfile() method instead.
>
> It will advance the file pointer.

Thank you, I'll change it. On a related matter, I seem to be making a
mistake somewhere in the way I'm importing and using modules (in
particular the "array" module).

The following code generates an error (traceback message follows
code):

import pdb
import array
from numpy import *

class MyClass:

    def __init__(self, initialData):

        test = array.array('f')  # Trigger error message



The traceback message is:

test = array.array('f')
AttributeError: 'builtin_function_or_method' object has no attribute
'array'

According to the Python 2.5 documentation for the "array" module it
does have an "array" method. I'm closely following examples I've found
online (here, for ex: http://www.python.org/search/hypermail/python-1993/0393.html)
but don't see my error.

This error goes away if I change the import statement to:

import pdb
from array import *
from numpy import *

and the instruction to

test = array('f')

but then I get another error (sorry I don't have it handy) that
implies Python is interpreting the object returned from "array('f')"
as a numpy.ndarray object. Something I'm doing is causing ambiguity it
seems.

Anyone? As always, thank you for the help.

L



More information about the Python-list mailing list