Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python(winreg)을 사용한 Windows 레지스트리 액세스

<시간/>

다재다능한 언어이자 매우 많은 수의 사용자 지원 모듈을 사용할 수 있기 때문에 파이썬도 OS 수준 프로그래밍에 능숙하다는 것을 알았습니다. 이 기사에서는 Python이 Windows 운영 체제의 레지스트리에 액세스하는 방법을 살펴보겠습니다.

winreg라는 모듈을 가져와야 합니다. 파이썬 환경으로.

아래 예에서는 winreg 모듈을 사용하여 먼저 ConnectRegistry 기능을 사용하여 레지스트리에 연결한 다음 OpenKey 기능을 사용하여 레지스트리에 액세스합니다. 마지막으로 액세스한 키의 결과를 인쇄하는 for 루프를 설계합니다.

import winreg
#connecting to key in registry
access_registry = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE)

access_key = winreg.OpenKey(access_registry,r"SOFTWARE\Microsoft\Windows\CurrentVersion")
#accessing the key to open the registry directories under
for n in range(20):
   try:
      x =winreg.EnumKey(access_key,n)
      print(x)
   except:
      break

출력

위의 코드를 실행하면 다음과 같은 결과가 나타납니다.

ApplicationFrame
AppModel
Appx
Audio
Authentication
AutoRotation
BITS
Casting
ClosedCaptioning
CloudExperienceHost
Component Based Servicing
……..
…..