Editing text with an external editor in Python

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Sep 1 20:23:01 CEST 2014


Chris Angelico wrote:

> On Tue, Sep 2, 2014 at 2:11 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> Anyone able to test it on Windows for me please?
>>
> 
> Seems to partially work. I added an 'import os' at the top, and a
> simple test call to the function, and it did give me my editor (nano)
> and retrieved the text. It did give a warning, though:
> 
> ----
> C:\>Python34\python 123123123.py
> cygwin warning:
>   MS-DOS style path detected: C:\DOCUME~1\M\LOCALS~1\Temp\tmp94rcwd57
>   Preferred POSIX equivalent is: /DOCUME~1/M/LOCALS~1/Temp/tmp94rcwd57

That's arguably a Python bug. Under Cygwin, it should use POSIX paths rather
than Windows paths.

I believe that sys.platform tells you if you are running under Cygwin.


> Windows doesn't have a nice $EDITOR environment variable to call on,

Why not? It's your environment, you can create any environment variable you
like, even under Windows, right?


> so I'm not sure what the best way to actually choose an editor is. I
> would hope that you can make it externally configurable 

Read $VISUAL, if it exists, otherwise $EDITOR, if it exists, otherwise fall
back on something hard coded. Or read it from an ini file. Or create an
entry in the register. Whatever. That's up to the application which uses
this function, not the function itself.

Under XP and older the standard DOS editor is EDIT, but that's gone from
Windows 7. Believe it or not, I understand that you can use:

    copy con [filename.???]

to do basic line editing, which is terrifying, but I believe it works.

http://stackoverflow.com/a/22000756

And of course, you can install whatever third-party editors you like. There
are Windows ports of nano, vim and emacs.

But fundamentally, the de facto "standard editor" on Windows is Notepad.


> You'll also have to cope with some other possibilities. What happens
> if someone tries Notepad? (Don't try this at home. We are experts and
> are testing on a closed track. Do not use Notepad unless you, too,
> have thirty years of special effects experience.) Turns out it doesn't
> like working with a file that another process has open. 

Ah, I feared that would be the case. I'll have to think about a way around
that. It won't be as neat, or as secure, but it should be doable.



-- 
Steven




More information about the Python-list mailing list