[Tutor] How to programmatically EDIT a python file using an editorof my choice ?

Alan Gauld alan.gauld at btinternet.com
Sun Apr 12 09:45:42 CEST 2009


"Dominique" <mydomdom at gmail.com> wrote

> What I'd like is to only edit the file using Idle or Notepad++ or Spe...
> What I'd like is to open the file using an editor of my choice.

How would you do this non programatically and not from the GUI?
In other words can you do it from an OS command line?

If so put that command in a call to subprocess.Popen.

If not is thre an API to the editor, for example Windows editors 
may have a COM interface that you could access.

But without being specific about the editor and the operating 
system it is hard to be specific about a solution. Generically,
if on Unix, you should probably respect the EDITOR or VISUAL 
environment variables since they reflect the users editor 
preferences so the code should look like:

ed = os.getenv('EDITOR')
vi = os.getenv('VISUAL')
editor = vi or ed
if editor:
   subprocess.Popen().call([editor, filename])

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list