code: def write_in_tmpfile(text, tmpfile):
          
          try:
              tmpfile.write(text)
          except TypeError:
              tmpfile.write(str.encode(text))
          except UnicodeEncodeError:
              tmpfile.write(text.encode("utf-8"))
docs: Write the given text in the given tmpfile in python2 and python3.