我可以使用什么程序来获取跨平台(或至少 Windows 或 Mac)使用 Python 的已安装软件列表?请检查我在 Google 上搜索的代码,但出现了一些错误。我使用的是 Python 2.7、win 7 64 位、eclipse
import StringIO
import traceback
import wmi
from _winreg import (HKEY_LOCAL_MACHINE, KEY_ALL_ACCESS,
OpenKey, EnumValue, QueryValueEx)
softFile = open('softLog.log', 'w')
errorLog = open('errors.log', 'w')
r = wmi.Registry ()
result, names = r.EnumKey (hDefKey=HKEY_LOCAL_MACHINE, sSubKeyName=r"Software\Microsoft\Windows\CurrentVersion\Uninstall")
softFile.write('These subkeys are found under "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"\n\n')
errorLog.write("Errors\n\n")
separator = "*" * 80
keyPath = r"Software\Microsoft\Windows\CurrentVersion\Uninstall"
for subkey in names:
try:
softFile.write(separator + '\n\n')
path = keyPath + "\\" + subkey
key = OpenKey(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS)
try:
temp = QueryValueEx(key, 'DisplayName')
display = str(temp[0])
softFile.write('Display Name: ' + display + '\nRegkey: ' + subkey + '\n')
except:
softFile.write('Regkey: ' + subkey + '\n')
except:
fp = StringIO.StringIO()
traceback.print_exc(file=fp)
errorMessage = fp.getvalue()
error = 'Error for ' + key + '. Message follows:\n' + errorMessage
errorLog.write(error)
errorLog.write("\n\n")
softFile.close()/* files are empty*/
errorLog.close()
控制台中显示的错误:
Traceback (most recent call last):
File "C:\Users\Administrator\EclipseWorkspace\Aug28th2012\PythonDevelopment\src\TestClass\Hii.py", line 21, in <module>
result, names = r.EnumKey (hDefKey=HKEY_LOCAL_MACHINE, sSubKeyName=r"Software\Microsoft\Windows\CurrentVersion\Uninstall")
File "C:\Python27\lib\site-packages\wmi.py", line 431, in __call__
handle_com_error ()
File "C:\Python27\lib\site-packages\wmi.py", line 241, in handle_com_error
raise klass (com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147352567, 'Exception occurred.', (0, u'SWbemProperty', u'Type mismatch ', None, 0, -2147217403), None)>
答案1
安装的软件概念没有明确定义,因为在 Windows 上,应用程序安装过程在非封闭平台(iOS、Android)上是自由形式的。
可能的选择
使用 Windows API 读取已安装的应用程序
列出文件夹
C:\Program Files
在 OSX 上,“已安装的应用程序”是文件夹的内容
/Applications
如果您想检查特定的应用程序,请从 Windows 注册表中读取其相关的安装条目(特定于应用程序)。
答案2
基本上,这似乎是 _winreg 常量和 64 位 python 的问题。