[SciPy-user] array.tofile stops writing to file

Stefan van der Walt stefan@sun.ac...
Wed Sep 26 13:44:24 CDT 2007


Hi Joe

On Wed, Sep 26, 2007 at 01:11:09PM +0200, J. K. wrote:
> I am using SciPy 0.6.0 with NumPy 1:1.03-1ubuntu2 on Python 2.5.1.5ubuntu3.
> 
> I am using arrays (about 120 rows with 7 columns) to store data and
> calculate stuff with the stored values. To be able to cross check the
> calculations, I wanted to store the arrays in files.
> I tried the different approaches:
> 
> - Array.tofile(FileName, sep='\t', format = "%s")
> 
> - Iteration over array
> k = 0
> for row in Koeff:
> 	Filename.write(array[k, 0], array[k, 1], array[k, 3])
> 	k = k + 1

Is "FileName" above a file descriptor?  If so, did you remember to
close the file?

I tested writing a random array to file using

x = N.random.random((120,7))
x.tofile('/tmp/data.txt',sep='\t',format="%s")

as well as

f = open('/tmp/data.txt','w')
x.tofile(f,sep='\t',format="%s")
f.close()

without a problem.

Under Python 2.5 you can conveniently do

with open('/tmp/data.txt') as f:
    x.tofile(f,sep='\t',format="%s")

Cheers
Stéfan


More information about the SciPy-user mailing list