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

牧云@^-^@ 提交于 2021-02-08 06:38:36

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/18044937/is-there-a-way-to-find-the-path-of-an-application-with-standard-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!