0

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()
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by woooee
1

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

Edited by woooee: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.