![](/img/trans.png)
[英]How do I read / write a file in Python (3) on Windows without introducing carriage returns?
[英]How do I read Windows Registry file to check for values? [Python]
我正在嘗試對Windows注冊表文件(.reg文件,脫機)執行審核檢查,我希望可以利用Python對reg文件進行檢查。
例如(偽代碼):
#Configure registry policy processing: Do not apply during periodic background processing
testloc = "C:\\Users\\test.reg"
datafile = open(testloc, "r")
read = datafile.read()
find(Software\Policies\Microsoft\Windows\Group Policy\{35378EAC-683F-11D2-A89A-00C04FBBCFA2})
check(NoBackgroundPolicy) #check if dword value is correct
if(dword == correct):
print("correct")
else:
print("wrong")
我嘗試查看_winreg,但似乎它在使用Windows API的實時系統上進行了檢查。 另一個問題是.reg文件大(〜200MB)。
如何使用Python執行這種檢查?
我不知道是否有一個庫可以專門讀取.reg文件,但是從外觀上看,它只是一個INI文件,頂部帶有附加版本信息。
這是如何使用configparser
模塊的示例。 一些注意事項:
ConfigParser
,一個readline
會跳過版本信息(類似於Windows Registry Editor Version 5.00
)。 否則將導致MissingSectionHeaderError
。 import configparser
testloc = "C:\\Users\\test.reg"
regdata = configparser.ConfigParser()
with open(testloc, "r", encoding="utf-16") as f:
f.readline() # skip version info in first line
regdata.read_file(f)
key = regdata[r"HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Group Policy\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}"]
value = key['"NoBackgroundPolicy"']
print(value)
但是,以這種方式進行操作可能會有一些缺點,例如,如何格式化您獲得的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.