[Tutor] How to programmatically EDIT a python file using an editorof my choice ?
Alan Gauld
alan.gauld at btinternet.com
Sun Apr 12 17:02:29 CEST 2009
"Dominique" <mydomdom at gmail.com> wrote
> I tried several other ways without any success:
> subprocess.Popen(args = ["notepad++.exe", filename])
> subprocess.Popen(args = ["C:\Program Files\Notepad++\notepad++.exe",
> filename])
>
> I wonder if it is due to the location of the softwares.
In a way. I think it's because you are using single backslashes.
You need to make the command a raw string by putting an r in front.:
subprocess.Popen([r"C:\Program Files\Notepad++\notepad++.exe", filename])
Otherwise Python gets confused by \n and thinks its a newline character
>>If not is thre an API to the editor, for example Windows editors
>>may have a COM interface that you could access.
>
> Do you mean that it is not always possible to open another software from
> python,
> notably if the software doesn't have a COM interface ?
You can always open the program but some programs may not take
command line arguments to open a file. Thuis you need to drive them
via COM to effectively use the File->Open menu option from Python.
Fortunately most editors will accept a command line filename so its
only rarely an issue for editors. But things like statistics [packages,
graphics editors etc can be more difficult.
>> ed = os.getenv('EDITOR')
>> vi = os.getenv('VISUAL')
>
> os.getenv is always None,
Presumably you don;t have either EDITOR or VISUAL defined.
What if you try
os.getenv('PATH')
That should return something.
> which may be correct since I am on Windows ?
No, getenv() works on any OS, although Unix tends to use environment
variables much more than Windows, which favours the registry.
Alan G.
More information about the Tutor
mailing list