[Numpy-discussion] fromfile and tofile access with a tempfile.TemporaryFile()

Travis Oliphant oliphant at ee.byu.edu
Tue Dec 12 16:26:44 CST 2006

Tim Hirzel wrote:

>Good thought Chris,
>Normal reading and writing does seem to work. .. 
>But, my friend Daniel figured out a workaround when I asked to confirm 
>this behavior on his windows setup (and it is does behave the same for 
>him).  The first clue was this:
> >>> f = tempfile.TemporaryFile()
> >>> type(f)
><type 'instance'>
> >>> g = open("temp","w+b")
> >>> type(g)
><type 'file'>
>so Daniel did a dir(f) and found the 'file' attribute
>so if you do (where 'a' is a numpy array)
> >>> a.tofile(f.file)
>It works!
>writing to the "file" attribute of the TemporaryFile, it works fine!  So 
>that's good, but still a little hinky.  Especially since it works on 
>on a linux platform, what does type(tempfile.TemporaryFile()) return?  I 
>assume an <type 'instance'>  as well...
>anways, so at least there is a quick repair for now.  Good news is, I 
>assume using 'f.file' would work on linux too in terms of having a 
>single cross-platform solution.
There is no file attribute on Linux.   On linux you get

 >>> type(f)
<type 'file'>

So, you might have to do something like:

if not isinstance(f, file):
   f = f.file

before passing f to the tofile method.

It seems to me that the temporary file mechanism on Windows is a little 


More information about the Numpy-discussion mailing list