pythonwindowsinstallation-path

Is there a way to find the path of an application with standard libraries?


I'd like to know if it is possible to find the installation directory of an application under Windows 7, such as MS Excel, with standard python 2.7 libraries. I mean, it shouldn't use any pywin32, or xlrd etc.

Maybe it will look up registry to find the installation path?


Solution

  • It might be quite tricky, however one approach would be to search for the launcher exe location in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\foo.exe

    Thusly something like this (I do not have Windows on this computer, so edits are welcome if bugs found ;), code should be Python 2 and 3 compatible):

    try:
        import winreg
    except ImportError:
        import _winreg as winreg
    
    handle = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
        r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe")
    
    num_values = winreg.QueryInfoKey(handle)[1]
    for i in range(num_values):
        print(winreg.EnumValue(handle, i))
    

    On Python 2 the module is named _winreg, but winreg on Python 3.