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

Charles R Harris charlesr.harris at gmail.com
Tue Dec 12 18:09:11 CST 2006


On 12/12/06, Travis Oliphant <oliphant at ee.byu.edu> wrote:
>
> 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
> >linux...
> >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
> odd.


Looks like a tempfile bug to me. Python should be cross platform and since
the file attribute is correctly set on windows, I don't see why the tempfile
can't be made to behave correctly.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061212/2b585b5d/attachment.html 


More information about the Numpy-discussion mailing list