使用Python Winreg模块操作注册表时报错解码问题的解决方法

  如下列代码用于读取注册表中网络的信息

1 import winreg
2 import sys
3 
4 unmanaged_networks = 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Signatures\Unmanaged'
5 try:
6     key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,unmanaged_networks)
7     print(type(key))
except Exception as e:
    print(e)
    sys.exit()

  运行以后,会报下面的错误:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 44-45: malformed \N character escape

  解决方法是:将键名字符串前面加上r,就可以消除这个错误。即

 1 import winreg
 2 import sys
 3 
 4 unmanaged_networks = r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Signatures\Unmanaged'
 5 try:
 6     key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,unmanaged_networks)
 7     print(type(key))
 8  
 9     
10 except Exception as e:
11     print(e)

 

posted @ 2022-05-20 11:47  Jason_huawen  阅读(157)  评论(0编辑  收藏  举报