Skip to content

Instantly share code, notes, and snippets.

@daGrevis
Last active January 2, 2016 11:39
Show Gist options
  • Select an option

  • Save daGrevis/8297886 to your computer and use it in GitHub Desktop.

Select an option

Save daGrevis/8297886 to your computer and use it in GitHub Desktop.
Edit text with editor
import os
import subprocess
import tempfile
def edit_text_with_editor(text=""):
_, location_for_temp_file = tempfile.mkstemp()
with open(location_for_temp_file, "r+") as temp_file:
temp_file.write(text)
temp_file.flush()
temp_file.seek(0)
subprocess.call([os.environ['EDITOR'], location_for_temp_file])
edited_text = temp_file.read()
os.remove(location_for_temp_file)
return edited_text
print(edit_text_with_editor("Hello, __!"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment