Command prompt not shown when running Python script with subprocess on Windows

ps16thypresenceisfullnessofjoy at gmail.com ps16thypresenceisfullnessofjoy at gmail.com
Wed Jun 4 16:25:20 EDT 2014


Thanks again for your help. I tried something similar to what you suggested:

def run_app(self, app_path):
    args = shlex.split(app_path.replace("\\", "\\\\"))
    args = [arg.replace("\\\\", "\\") for arg in args]
    args[0] = os.path.expandvars(args[0])
    try:
        if pywin32:
            exe = win32api.FindExecutable(args[0])[1]
            if exe != os.path.abspath(args[0]):
                args.insert(0, exe)
        subprocess.Popen(args)
    except Exception:
        return False
    return True

where pywin32 is a global variable defined earlier in my program based on whether or not the win32api module is available.
This code generally works quite well, but with one problem. I have in my XML file a line like this:

<app name="Notepad++" checked="false">%ProgramFiles%\Notepad++\notepad++.exe C:\Users\Timothy\Documents\Python\StartupPlus\apps.xml</app>

Within the try statement, the variable 'args' contains this list, as expected:

['C:\Program Files (x86)\Notepad++\notepad++.exe', 'C:\Users\Timothy\Documents\Python\StartupPlus\apps.xml']

But the 'exe' variable returned by win32api.FindExecutable is not what I would expect:

C:\PROGRA~2\NOTEPA~1\NOTEPA~1.EXE

Because of this, the boolean comparison:

if exe != os.path.abspath(args[0]):

returns True when it should return False, since it is comparing a short (MS-DOS) path name with a long path name.
Should I use win32file.GetLongPathName to fix this problem, or do you know of a better solution?

-- Timothy



More information about the Python-list mailing list