使用Python检测Windows主机已安装程序列表

#!/usr/bin/python
 
import os
import socket
import _winreg
 
#检测主机名,并将主机名作文检测结果的文件名
hostname = socket.gethostname()
file = open(r'd:\%s.txt' % hostname, 'a')
 
#定义检测位置
sub_key = [r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', r'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall']
 
software_name = []
adobe = 'adobe'
for i in sub_key:
    key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, i, 0, _winreg.KEY_ALL_ACCESS)
    for j in range(0, _winreg.QueryInfoKey(key)[0]-1):
        try:
            key_name = _winreg.EnumKey(key, j)
            key_path = i + '\\' + key_name
            each_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key_path, 0, _winreg.KEY_ALL_ACCESS)
            DisplayName, REG_SZ = _winreg.QueryValueEx(each_key, 'DisplayName')
            DisplayName = DisplayName.encode('utf-8')
            software_name.append(DisplayName)
        except WindowsError:
            pass
 

software_name = list(set(software_name))
software_name = sorted(software_name)
 
for result in software_name:
    #检测Adobe软件的安装
    if 'Adobe' in result:
        file.write(result + '\n')

转载于:https://www.cnblogs.com/flankershen/p/7568828.html

你可能感兴趣的:(使用Python检测Windows主机已安装程序列表)