Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created June 9, 2011 21:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanorelli/1017770 to your computer and use it in GitHub Desktop.
Save jordanorelli/1017770 to your computer and use it in GitHub Desktop.
A Python script to open a temporary file in the user's editor, echo its contents, and delete the file.
# Jordan Orelli wrote it.
# Do what you want with it.
# June 9th, 2011
import os
import subprocess
import tempfile
if not os.environ.has_key('EDITOR'):
raise Exception('Unable to open editor; please set your EDITOR '
'environment variable to point to your preferred '
'editor, e.g. "/usr/bin/vim" or simply "vim"')
editor = os.environ['EDITOR']
fd, filename = tempfile.mkstemp(text=True)
cmd = '%s %s' % (editor, filename)
write_status = subprocess.call(cmd, shell=True)
if write_status != 0:
os.remove(filename)
raise Exception("The editor returned a non-zero status "
"(that means it failed.)")
f = open(filename)
print f.read()
f.close()
os.remove(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment