Listing 8. Creating Temporary Files

#! /usr/local/bin/python
import os
import tempfile
import sys
try:
    t_path = tempfile.mktemp()
    ttt = os.open(t_path, os.O_CREAT | os.O_RDWR)
    # O_RDWR allows read & write
except IOError:
    print 'Error:  Couldn\'t create temp file.'
    sys.exit(0)
# always close and remove the temp file
# before you exit from script
os.close(ttt)
os.remove(t_path)