def get_browser_path(): import win32con, win32api upper_keyword = '\chrome.exe' path = None sub_key = r'Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Store' key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, sub_key, 0, win32con.KEY_READ) info = win32api.RegQueryInfoKey(key) for i in range(0, info[1]): value = win32api.RegEnumValue(key, i) print(value[0]) if value[0].lower().endswith(upper_keyword.lower()): path = value[0] break win32api.RegCloseKey(key) return path
def get_browser_path1(browser): # 获取windows 路径方式1 import winreg result = [] # 需要遍历的两个注册表 sub_key = [r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', r'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall', ] for i in sub_key: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, i) for j in range(winreg.QueryInfoKey(key)[0]): try: each_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, f'{i}\\{winreg.EnumKey(key, j)}') display_path, reg_sz = winreg.QueryValueEx(each_key, "DisplayIcon") if browser in display_path: result.append(display_path.split(',')[0]) except WindowsError: pass return result def get_browser_path2(browser): # 获取windows 路径方式2 # 连接注册表根键 以HKEY_LOCAL_MACHINE为例 regRoot = ConnectRegistry(None, HKEY_LOCAL_MACHINE) subDir = r'SOFTWARE' # 获取指定目录下所有键的控制 keyHandle = OpenKey(regRoot, subDir) # 获取该目录下所有键的个数(0-下属键个数 1-当前键值个数) count = QueryInfoKey(keyHandle)[0] lists = [] for i in range(count): # 穷举键获取键名 subKeyName = EnumKey(keyHandle, i) subDir_2 = r'%s\%s' % (subDir, subKeyName) # 根据获取的键名拼接之前的路径作为参数 获取当前键下所属键的控制 keyHandle_2 = OpenKey(regRoot, subDir_2) num = QueryInfoKey(keyHandle_2)[1] for j in range(num): name, value, type_ = EnumValue(keyHandle_2, j) if browser in name.lower(): _browser_regs = '\\'.join(value.split('\\')[:-1]) + '\\DefaultIcon' key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, _browser_regs) value1, _type1 = winreg.QueryValueEx(key, "") lists.append(value1.split(',')[0]) # 读写操作结束后关闭键 CloseKey(keyHandle_2) return lists def get_browser_path3(): # 获取windows 路径方式3 result = None if winreg: for subkey in ['ChromeHTML\\\\shell\\\\open\\\\command', 'Applications\\\\chrome.exe\\\\shell\\\\open\\\\command']: try: result = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, subkey) except WindowsError: pass if result is not None: result_split = shlex.split(result, False, True) result = result_split[0] if result_split else None if os.path.isfile(result): break result = None else: expected = "google-chrome" + (".exe" if os.name == 'nt' else "") for parent in os.environ.get('PATH', '').split(os.pathsep): path = os.path.join(parent, expected) if os.path.isfile(path): result = path break return [result] def get_browser_path4(path, file_list=[]): # 获取windows 路径方式4 try: os.listdir(path) except: return for i in os.listdir(path): path1 = os.path.join(path, i) if os.path.isdir(path1) and 'program' in path1.lower(): get_browser_path4(path1, file_list) elif os.path.isfile(path1): if 'google' in path1.lower() and path1.lower().endswith('chrome.exe') and 'temp' not in path1.lower(): file_list.append(path1.replace('\\\\', '\\')) return file_list return file_list def get_soft_path(path, file_list=[]): # mac 获取谷歌浏览器的路径 for i in os.listdir(path): path1 = os.path.join(path, i) if os.path.isdir(path1): get_soft_path(path1, file_list) elif os.path.isfile(path1): if 'MacOS' in path1 and path1.endswith('Chrome'): file_list.append(path1) return file_list return file_list
browser = 'chrome' if sys.platform.lower().startswith('freebsd') or sys.platform.lower().startswith('darwin'): path = get_soft_path('/Applications/') else: path = get_browser_path2(browser) or get_browser_path1(browser) or get_browser_path3() or get_browser_path4(r'C:\\') print(path)
#获取mac地址文章来源地址https://www.toymoban.com/news/detail-513525.html
def get_network_info(): #https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapter import wmi mac = '' s = wmi.WMI() for nw in s.Win32_NetworkAdapter(): # print(nw.properties) if '以太网' in str(nw.NetConnectionID): mac = nw.MACAddress.replace(':', '-') return mac def get_netcard(): import uuid from psutil import net_if_addrs mac = '' info = net_if_addrs() for k, v in info.items(): if k == '以太网': k = '以太网en0' print(k) if k not in ['以太网', 'en0'] and '以太网' not in str(k): continue for i in v: if '-' in i[1] or ':' in i[1]: if len(i[1]) == 17: mac = str(i[1]).lower() break if not mac: node = uuid.getnode() mac = uuid.UUID(int=node).hex[-12:] mac = '-'.join([mac[i:i + 2] for i in range(0, len(mac) + 2 // 2, 2)])[:-1] return mac mac = get_network_info() print(mac) print(get_netcard())
文章来源:https://www.toymoban.com/news/detail-513525.html
到了这里,关于windows 及Mac os 获取谷歌浏览器路径的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!