[Numpy-discussion] fromfile and tofile access with a tempfile.TemporaryFile()
hirzel at resonon.com
Wed Dec 13 15:28:02 CST 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.
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
> >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
> 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.
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
More information about the Numpy-discussion