Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39464 > unrolled thread

Re: NamedTemporaryFile does not match documentation

Started bySerhiy Storchaka <storchaka@gmail.com>
First post2013-02-21 23:30 +0200
Last post2013-02-21 23:30 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: NamedTemporaryFile does not match documentation Serhiy Storchaka <storchaka@gmail.com> - 2013-02-21 23:30 +0200

#39464 — Re: NamedTemporaryFile does not match documentation

FromSerhiy Storchaka <storchaka@gmail.com>
Date2013-02-21 23:30 +0200
SubjectRe: NamedTemporaryFile does not match documentation
Message-ID<mailman.2200.1361482221.2939.python-list@python.org>
On 21.02.13 22:46, Jason Friedman wrote:
> Python 3.2.2 (default, Feb 14 2012, 08:06:31)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from tempfile import NamedTemporaryFile
>>>> f = NamedTemporaryFile(delete=False)
>>>> f
> <tempfile._TemporaryFileWrapper object at 0x7f867e8bc050>
>>>> f.name
> '/tmp/tmpqxnd_4'
>>>> f.write("Hello World!\n")
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: 'str' does not support the buffer interface
>
> Or, maybe I am reading the documentation incorrectly.  My goal is to
> be able to write to that temporary file along the lines of "f.write()"
> or "with open(f) as writer ...".

NamedTemporaryFile by default opens a file in binary mode ('w+b'). Write 
bytes or specify text mode.

 >>> f.write(b"Hello World!\n")
13
 >>> ft = NamedTemporaryFile('w+', delete=False)
 >>> ft.write("Hello World!\n")
13

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web