[SciPy-user] fromfile, item, what other output than stdout / stderr is used ?

Robert Kern robert.kern@gmail....
Sun May 10 16:15:37 CDT 2009


On Sun, May 10, 2009 at 05:02, Stef Mientki <s.mientki@ru.nl> wrote:
> hello,
>
> I'm using "fromfile" to read data from a file generated by another program.
> The data is read on the same operating system as where the file was created.
> I use on several locations, at the procedure seems to run very well, but ...
> ... when run the program from the normal window command box,
> I get an error message:
>    "40 items requested but only 10 read"
>
> Normally I run all the programs with either a hidden command window or
> from some kind of IDE.
> In the IDE, stdout and stderr are redirected, and I see all print
> commands and errors in the redirection window,
> but I don't see the above error message from numpy.
> So it seems that numpy is using a third kind of output device.
> ???

IDLE redirects sys.stdout and sys.stderr. It does nothing with the C
stdout and stderr file handles. Presumably we are printing stuff out
from the C level using printf(). We probably should be using the
Python API function for printing to sys.std*. Patches welcome.

> Now from the documentation at
>  http://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html
> I read:
>
> numpy.fromfile(/file/, /dtype=float/, /count=-1/, /sep=''/)
>
> *count* : int
>
>    Number of items to read. -1 means all items (i.e., the complete file).
>
>
> What is a item ?
> Is it in bytes, or as in my case, where dtype is an 32 bit integer, in
> integers (which I would read from the documentation) ?

An item is one instance of the dtype.

> Well it must be a byte I guess,
> because if I use bytes as the count, the program is working well,
> but I get the above error message in the windows command window.

Since the number of bytes is greater than the number of items and you
are getting errors suggesting that you have requested more items than
there are available in the file, I really don't understand how this
sentence can be true.

In [1]: f = open('foo.dat', 'wb')

In [2]: f.write('\x01' * 40)

In [3]: f.close()

In [4]: !ls -l foo.dat
IPython system call: ls -l foo.dat
-rw-r--r--  1 rkern  staff  40 May 10 16:14 foo.dat

In [5]: from numpy import *

In [6]: fromfile('foo.dat', dtype=int32, count=10)
Out[6]:
array([16843009, 16843009, 16843009, 16843009, 16843009, 16843009,
       16843009, 16843009, 16843009, 16843009])

In [7]: fromfile('foo.dat', dtype=int32, count=40)
40 items requested but only 10 read
Out[7]:
array([16843009, 16843009, 16843009, 16843009, 16843009, 16843009,
       16843009, 16843009, 16843009, 16843009])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list