0

I've asked this question before but it was unclear (how do i find the directory to an installed program in python?) I want to list all installed programs and get their paths so I can ask the user which program they want to launch. my current script is this:

import winreg

def foo(hive, flag):
    aReg = winreg.ConnectRegistry(None, hive)
    aKey = winreg.OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                          0, winreg.KEY_READ | flag)

    count_subkey = winreg.QueryInfoKey(aKey)[0]

    software_list = []

    for i in range(count_subkey):
        software = {}
        try:
            asubkey_name = winreg.EnumKey(aKey, i)
            asubkey = winreg.OpenKey(aKey, asubkey_name)
            software['name'] = winreg.QueryValueEx(asubkey, "DisplayName")[0]

            try:
                software['version'] = winreg.QueryValueEx(asubkey, "DisplayVersion")[0]
            except EnvironmentError:
                software['version'] = 'undefined'
            try:
                software['path'] = winreg.QueryValueEx(asubkey, "InstallPath")[0]
            except EnvironmentError:
                software['path'] = 'undefined'
            try:
                software['publisher'] = winreg.QueryValueEx(asubkey, "Publisher")[0]
            except EnvironmentError:
                software['publisher'] = 'undefined'
            software_list.append(software)
        except EnvironmentError:
            continue

    return software_list

software_list = foo(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_32KEY) + foo(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_64KEY) + foo(winreg.HKEY_CURRENT_USER, 0)

for software in software_list:
    print('Name=%s, Version=%s, Publisher=%s, path=%s' % (software['name'], software['version'], software['publisher'], software['path']))
print('Number of installed apps: %s' % len(software_list))
print(winreg.HKEY_LOCAL_MACHINE)

what i was hoping one of the outputs would be name=some program version=some version publisher=some publisher path=c:/programfiles/long/path/to/exe/file/some program.exe my exception catcher puts undefined if it cant find the information. and unfortunately software['path'] = winreg.QueryValueEx(asubkey, "InstallPath")[0] doesn't work because the outputs of paths are all undefined:

Name=Python 3.8.5 Executables (32-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 Documentation (32-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Python Launcher, Version=3.9.7280.0, Publisher=Python Software Foundation, path=undefined
Name=Microsoft 365 Apps for enterprise - en-us, Version=16.0.13426.20308, Publisher=Microsoft Corporation, path=undefined
Name=VLC media player, Version=3.0.11, Publisher=VideoLAN, path=undefined
Name=Google Chrome, Version=87.0.4280.88, Publisher=Google LLC, path=undefined
Name=Python 3.9.1 Core Interpreter (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 Executables (64-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.1 Development Libraries (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.0 pip Bootstrap (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.0 Utility Scripts (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.0 Documentation (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.1 Tcl/Tk Support (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.0 Standard Library (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.0 Tcl/Tk Support (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.1 Utility Scripts (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.0 Add to Path (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.1 Add to Path (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.1 Documentation (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.1 Standard Library (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 Test Suite (64-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.1 Executables (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 Documentation (64-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Office 16 Click-to-Run Licensing Component, Version=16.0.13426.20308, Publisher=Microsoft Corporation, path=undefined
Name=Office 16 Click-to-Run Extensibility Component, Version=16.0.13426.20294, Publisher=Microsoft Corporation, path=undefined
Name=Office 16 Click-to-Run Localization Component, Version=16.0.13426.20294, Publisher=Microsoft Corporation, path=undefined
Name=Python 3.9.0 Test Suite (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.0 Core Interpreter (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 Standard Library (64-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.1 Test Suite (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.0 Executables (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 pip Bootstrap (64-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 Tcl/Tk Support (64-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 Utility Scripts (64-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 Core Interpreter (64-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.0 Development Libraries (64-bit), Version=3.9.150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.9.1 pip Bootstrap (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Name=Python 3.8.5 Development Libraries (64-bit), Version=3.8.5150.0, Publisher=Python Software Foundation, path=undefined
Name=Discord, Version=0.0.309, Publisher=Discord Inc., path=undefined
Name=CrewLink 1.1.6, Version=1.1.6, Publisher=Ottomated, path=undefined
Name=Microsoft OneDrive, Version=20.201.1005.0009, Publisher=Microsoft Corporation, path=undefined
Name=PyCharm Community Edition 2020.3, Version=203.5981.165, Publisher=JetBrains s.r.o., path=undefined
Name=Microsoft Teams, Version=1.3.00.30866, Publisher=Microsoft Corporation, path=undefined
Name=Python 3.9.1 (64-bit), Version=3.9.1150.0, Publisher=Python Software Foundation, path=undefined
Number of installed apps: 70

which means I need to find another solution for finding paths. any suggestions? I don't need versions or publishers. just the name and path.

MI HAEL
  • 81
  • 6
  • You can use `which` command for linux and `Get-Command` for Windows. Grab there results and you can use them – crackaf Dec 14 '20 at 21:59
  • what is `Get-Command`? please clarify – MI HAEL Dec 14 '20 at 22:52
  • Go through this [read](https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output). And there is `Get-Command` in windows PowerShell which returns the path of the application. Like if you input `Get-Command notepad`, the output will be something like this `C:\Windows\system32\notepad.exe`. – crackaf Dec 14 '20 at 23:18
  • Someone else posted a solution and that will work too. He may have deleted it but I don't know why. Here is the deleted [answer](https://pastebin.com/ifNfk0hG) – crackaf Dec 14 '20 at 23:21
  • get command only works if I can launch it using a command. you cant just say zoom.exe and it'll launch zoom. and for your second one only works if I have the registry for the path of the application. ill need a script to comb through registries to find executable registries. know one for that? – MI HAEL Dec 15 '20 at 18:23

0 Answers0