Difference between tempfile and spooled tempfile?
Steve Howell
showell30 at yahoo.com
Thu Apr 5 11:10:23 EDT 2012
On Apr 5, 7:50 am, "Alex van der Spek" <zd... at xs4all.nl> wrote:
> I do not understand why the spooled write gives an error. See below.
> The normal tempfile works just fine. They are supposed to behave equal?
>
> All insight you can provide is welcome.
> Alex van der Spek
>
> +++++++++++++++++++++++++++++++++++++++++++++++++
>
> Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)]
> on win32
> Type "copyright", "credits" or "license()" for more information.
>
> >>> import array
> >>> import tempfile
> >>> stf = tempfile.SpooledTemporaryFile(max_size=1024)
> >>> ptf = tempfile.TemporaryFile()
> >>> fff = [float(x) for x in range(2048)]
> >>> ffa = array.array('f',fff)
> >>> ptf.write(ffa)
> >>> stf.write(ffa)
>
> Traceback (most recent call last):
> File "<pyshell#7>", line 1, in <module>
> stf.write(ffa)
> File "C:\Python27\lib\tempfile.py", line 595, in write
> rv = file.write(s)
> TypeError: must be string or read-only character buffer, not array.array
>
I think the docs are slightly misleading. While SpooledTemporaryFile
allows you to write(), it's more finicky about serializing arrays,
hence the error message.
If you look under the hood, you'll see that it's mostly a limitation
of StringIO.
http://hg.python.org/cpython/file/2.7/Lib/tempfile.py
494 """Temporary file wrapper, specialized to switch from
495 StringIO to a real file when it exceeds a certain size or
496 when a fileno is needed.
497 """
498 _rolled = False
499
500 def __init__(self, max_size=0, mode='w+b', bufsize=-1,
501 suffix="", prefix=template, dir=None):
502 self._file = _StringIO()
503 self._max_size = max_size
504 self._rolled = False
505 self._TemporaryFileArgs = (mode, bufsize, suffix,
prefix, dir)
(See line 502.)
600 def write(self, s):
601 file = self._file
602 rv = file.write(s)
603 self._check(file)
604 return rv
(See line 602.)
I'm looking at a slightly different version of the module than you,
but hopefully you get the idea.
More information about the Python-list
mailing list