Opening vi from Python
<p>I want to replicate the functionality that happens when you do something like 'git commit'.  It opens up your editor and you type some stuff and then save/exit to hand off that file back to the script that launched the editor.</p>

<p>How would I implement this functionality in Python?</p>

<p>EDIT:</p>

<p>Thanks for the suggestions, here's a working example based on the answers:</p>

<pre><code>import os, subprocess, tempfile

(fd, path) = tempfile.mkstemp()
fp = os.fdopen(fd, 'w')
fp.write('default')
fp.close()

editor = os.getenv('EDITOR', 'vi')
print(editor, path)
subprocess.call('%s %s' % (editor, path), shell=True)

with open(path, 'r') as f:
  print(f.read())

os.unlink(path)
</code></pre>
