我实际上已经找到了一个解决方案,但它涉及全局变量,并且没有返回语句。我想知道是否有更好的办法。基本上,我做了一个递归函数来搜索注册表项,我希望它在找到匹配项时返回“key”对象。
这是我的第一次尝试:
import winreg
import sys
def parse_reg(key,find_subkey=" ",find_value=" "):
try:
num_subkeys = winreg.QueryInfoKey(key)[0] #queries number of subkeys for this key
for i in range(num_subkeys):
sub_keys = winreg.EnumKey(key,i) #gets the subkey name from index i
sub_key = winreg.OpenKeyEx(key,sub_keys) #subkey object from the subkey name
num_values = winreg.QueryInfoKey(sub_key)[1]
for j in range(num_values): #this for statement iterates through the values of the subkey
sub_values = winreg.EnumValue(sub_key,j)
print("-"*len(sub_keys)+sub_values[0]+": "+str(sub_values[1]))
if str(sub_values[1]) == find_value:
return sub_key
print(sub_keys)
if find_subkey in sub_keys:
return sub_key
parse_reg(sub_key,find_subkey,find_value) #call the function again, this time with the new subkey
except PermissionError as e:
pass
reg = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE)
key = winreg.OpenKeyEx(reg,r"System\CurrentControlSet\Enum")
key2 =parse_reg(key,"VID_2C7C&PID_0121&MI_03")
print(key2) #prints None
我没有意识到递归函数返回到它自己,所以这不起作用。
尝试2,使用参数解除递归函数调用:
import winreg
import sys
def parse_reg(key,_return,find_subkey=" ",find_value=" "):
if _return !=None:
sub_key = ""
return parse_reg(sub_key,_return,find_subkey,find_value) #tried this and return _return
try:
num_subkeys = winreg.QueryInfoKey(key)[0] #queries number of subkeys for this key
for i in range(num_subkeys):
sub_keys = winreg.EnumKey(key,i) #gets the subkey name from index i
sub_key = winreg.OpenKeyEx(key,sub_keys) #subkey object from the subkey name
num_values = winreg.QueryInfoKey(sub_key)[1]
for j in range(num_values): #this for statement iterates through the values of the subkey
sub_values = winreg.EnumValue(sub_key,j)
print("-"*len(sub_keys)+sub_values[0]+": "+str(sub_values[1]))
if str(sub_values[1]) == find_value:
_return =sub_key
print(sub_keys)
if find_subkey in sub_keys:
_return= sub_key
parse_reg(sub_key,_return,find_subkey,find_value) #call the function again, this time with the new subkey
except PermissionError as e:
pass
reg = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE)
key = winreg.OpenKeyEx(reg,r"System\CurrentControlSet\Enum")
key2 =parse_reg(key,None,"VID_2C7C&PID_0121&MI_03")
print(key2)
这一个永远叫它自己。如果我有 return _return
它的问题与第一个解决方案相同。
最后,我决定只使用一个全局变量。
import winreg
import sys
def parse_reg(key,_return,find_subkey=" ",find_value=" "):
try:
num_subkeys = winreg.QueryInfoKey(key)[0]
for i in range(num_subkeys):
sub_keys = winreg.EnumKey(key,i)
sub_key = winreg.OpenKeyEx(key,sub_keys)
num_values = winreg.QueryInfoKey(sub_key)[1]
for j in range(num_values):
sub_values = winreg.EnumValue(sub_key,j)
print("-"*len(sub_keys)+sub_values[0]+": "+str(sub_values[1]))
if str(sub_values[1]) == find_value:
global key2
key2 = sub_key
print(sub_keys)
if find_subkey in sub_keys:
print(sub_keys,find_subkey)
print("FOUND")
print(dir(sub_key))
global key2
key2=sub_key
parse_reg(sub_key,_return,find_subkey,find_value)
except PermissionError as e:
pass
reg = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE)
key = winreg.OpenKeyEx(reg,r"System\CurrentControlSet\Enum")
parse_reg(key,None,"VID_2C7C&PID_0121&MI_03")
print(key2,"hello")
我知道必须有一种方法来回报价值,但我被难住了。任何帮助都将不胜感激!!:)
暂无答案!
目前还没有任何答案,快来回答吧!