Hi,
I want to read binary data from a file using fromfile. This works as
long as I use a file name as argument to fromfile. With a file object
the data is wrong!
Consider the following example:
from numpy import *
fname='file.bin'
fname2='file2.bin'
a = arange(1, 30)
print type(a[0])
print \noriginal data
print a
# write to file name
a.tofile(fname)
# write to file object
f = open(fname2, 'w')
a.tofile(f)
f.close()
print \nWritten to file name, read from file name
b = fromfile(fname, dtype=int32)
print b
print \nWritten to file name, read from file object
f = open(fname, 'r')
b = fromfile(f, dtype=int32)
f.close()
print b
print \nWritten to file object, read from file name
b = fromfile(fname2, dtype=int32)
print b
print \nWritten to file object, read from file object
f = open(fname2, 'r')
b = fromfile(f, dtype=int32)
f.close()
print b
This prints:
type 'numpy.int32'
original data
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 27 28 29]
Written to file name, read from file name
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 27 28 29]
Written to file name, read from file object
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
0 0 0 0]
Written to file object, read from file name
[ 123456789 2573 2816 3072 3328 3584 3840
4096 4352 4608 4864 5120 5376 5632 5888 6144 6400 6656 6912 7168 7424]
Written to file object, read from file object
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
0 0 0 0]
Size of 'file.bin' (written to file name) is 116 Bytes, size of
'file2.bin' (written to file object) is 117 Bytes!
So the only correct data is when writing to a file name and reading from
a file name. All other variants yield wrong data!
This happens with Numpy 1.4.1 with Python 2.6 under Windows XP. For
comparison I only have a 1.1.0 Numpy with Python 2.5 under Linux Debian
where in all cases I get the same, correct arrays!
Am I doing something substantially wrong or is this a bug?
Christoph
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion