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

Tim Hirzel hirzel at resonon.com
Wed Dec 13 16:28:02 EST 2006


Thanks for everyone's help and thoughts.  I agree that this behavior is 
buggy.  I submitted a bug report to the python project at sourceforge, 
with a link to this thread.  Hopefully the report will be helpful.

tim

Charles R Harris wrote:
>
>
> On 12/12/06, *Travis Oliphant* <oliphant at ee.byu.edu 
> <mailto: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
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   



More information about the NumPy-Discussion mailing list