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

Dominique mydomdom at gmail.com
Mon Apr 13 11:07:19 CEST 2009

Sander Sweers <sander.sweers <at> gmail.com> writes:

> 2009/4/13 Dominique <mydomdom <at> gmail.com>:
> > What surprises me is that before calling the subprocess.Popen() method, I
> > normalized the path using:
> > filename = os.path.normpath(filename).
> It does.But do you also call os.path.normpath for the program path?
> See below an example.
> >>> import os
> >>> path = 'C:\Program Files\Notepad++\notepad++.exe'
> >>> os.path.normpath(path)
> 'C:\\Program Files\\Notepad++\notepad++.exe'
> Greets
> Sander
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> http://mail.python.org/mailman/listinfo/tutor

You are perfectly right. I forgot to do it also for the program path ! Thanks.

1) Code directly in the console:
In addition, the print instruction alters the path because of the backslash and
the n:

>>> import os.path
>>> path = 'C:\Program Files\Notepad++\notepad++.exe'
>>> path
'C:\\Program Files\\Notepad++\notepad++.exe'
>>> print path
C:\Program Files\Notepad++

2) Code inside a wxPython application:
But inside a wxPython app, the following code:
import os.path
import subprocess as s
path = 'C:\Program Files\Notepad++\notepad++.exe'
print "path before = ",path
path = os.path.normpath(path)
print "path after = ", path

path before =  C:\Program Files\Notepad++
path after =  C:\Program Files\Notepad++
And of course it raises an exception when trying to open notepad++.

If I add r before the path or 2 backslashes inside the path, everything is ok.
It returns:
path before =  C:\Program Files\Notepad++\notepad++.exe
path after =  C:\Program Files\Notepad++\notepad++.exe
And Notepad++ is opened.

Note that the path returned only contains 1 backslash, as opposed to what is
returned if it is coded directly in the console.
That is what confused me.
I think it should be linked to wxPython.

Thanks a lot for your explanations and your time.

More information about the Tutor mailing list