For some reason, I can create temp files but I cannot write to them. Nothing is saved, and the file is always empty. What am I doing wrong? I've tried making it in /tmp, Ive specified mode=w+b, they all do the same. (nothing)

import tempfile
def main():
  test = tempfile.NamedTemporaryFile()
  test.write('asdfsadfsadfsadfasdfasdfsadfsdfsadfsadfs')
  res=open(test.name,"r")
  print res.read()
  print test.name
  return

if __name__ == '__main__':
  main()

The pointer is positioned at the end of the file so nothing is read.

import tempfile
if __name__ == '__main__':
  test = tempfile.NamedTemporaryFile()
  test.write('asdfsadfsadfsadfasdfasdfsadfsdfsadfsadfs')
  test.seek(0)                      ## offset=0 --> beginning of file
  print "\n test read", test.read()
  print test.name
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.