0

I use python2.7 and i have a question about reading from tempfile. Here is my code:

import tempfile


for i in range(0,10):
    f = tempfile.NamedTemporaryFile()

    f.write("Hello")

    ##f.seek(0)

    print f.read()

With this code , i get something like this:

Rワ
nize.pyR
゙`Sc
d
Rワ
Rワ
Z
Z
nize.pyR
゙`Sc

what are these?

Thanks!

AlexDotis
  • 324
  • 4
  • 11

1 Answers1

1

You are writing string to a file opened in bytes mode. Add the mode parameter to your call to NamedTemporaryFile:

f = tempfile.NamedTemporaryFile("w")

See https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

  • if I do this in code then displays an error : `IOError: File not open for reading`.. There is something i dont understand? – AlexDotis Dec 08 '16 at 15:55