Does array.read() move file pointer automatically?

L

Lionel

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?

Thanks everyone.
L
 
M

MRAB

Lionel said:
> 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.
 
L

Lionel

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
 
M

MRAB

Lionel said:
>
> 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 *
>
[snip]
I think that numpy has a class called "array", so the "import *" will
result in the name "array" binding to that, thus hiding the module
called "array" that you've only just imported!
 
L

Lionel

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 *
 >
[snip]
I think that numpy has a class called "array", so the "import *" will
result in the name "array" binding to that, thus hiding the module
called "array" that you've only just imported!

I believe that's correct. I've gotten it to work by changing the
import statements to:

import pdb
import array
import numpy

and then I qualify the module specific attributes as either
"numpy.SomeAttribute" or "array.SomeAttribute" to avoid this namespace
ambiguity.

Thanks for the input!

L
 
J

John Machin

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?

Suggestions for the the next time you have a question like that:

(1) Consider which outcome is more plausible, write your code
accordingly and see what happens

(2) If it blows up, or if you like looking before you leap anyway, try
inserting temporarily some code -- in this case
print MyFileHandle.tell()
that will tell you the answer to your question.

In this game you are allowed to experiment to get an answer, and it's
often faster than phone-a-friend :)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top