2014-04-11 64 views
0

我正在尋找一種方法來檢查是否存在與python的註冊表項。檢查是否存在與python的註冊表項

我該怎麼做,或者我需要檢查一下注冊表項是否存在?

+0

它似乎有一個答案[這裏](http://stackoverflow.com/a/5227427/3482069)。 –

+0

@ edwin -s這個cheack註冊表項存在還是註冊表存在? – user1738413

+0

我會在答案中提供更多詳細信息。 –

回答

1

在以前的回答中似乎有一些信息here

您是否正在檢查它的存在,因爲您希望程序讀取它?要檢查它們是否存在,可以將其包裝在try-except塊中。這將防止嘗試讀取密鑰的「競爭條件」,在(不太可能)事件中,它會在檢查其存在和實際讀取密鑰之間進行修改。例如:

from _winreg import * 

key_to_read = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall' 

try: 
    reg = ConnectRegistry(None, HKEY_LOCAL_MACHINE) 
    k = OpenKey(reg, key_to_read) 

    # do things with the key here ... 

except: 
    # do things to handle the exception here 
+0

不工作,你忘了 REG = ConnectRegistry(無,HKEY_LOCAL_MACHINE) 必須 AREG = ConnectRegistry(無,HKEY_LOCAL_MACHINE) – user1738413