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

Windows 시작 시 Python 스크립트를 자동 실행하시겠습니까?

<시간/>

Windows 시작에 Python 스크립트를 추가하는 것은 기본적으로 Windows가 부팅될 때 Python 스크립트가 실행됨을 나타냅니다. 이는 두 단계 프로세스를 통해 수행할 수 있습니다. -

1단계:Windows 시작 폴더에 스크립트 추가 또는 추가

윈도우 부팅 후 시작 폴더나 디렉터리에 있는 모든 응용 프로그램을 실행(더블 클릭과 동일)합니다.

주소

C:\Users\current_user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\

기본적으로 현재 사용자 아래의 AppData 디렉토리 또는 폴더는 숨겨져 있어 숨겨진 파일이 이를 가져와 스크립트의 바로 가기를 지정된 주소 또는 스크립트 자체에 붙여넣을 수 있습니다. 이 외에도 .PY 파일 기본값은 python IDE로 설정해야 합니다. 그렇지 않으면 스크립트가 실행되는 대신 텍스트로 열릴 수 있습니다.

2단계:Windows 레지스트리에 스크립트 추가 또는 추가

이 프로세스는 제대로 수행되지 않으면 위험할 수 있습니다. 여기에는 python 스크립트 자체에서 Windows 레지스트리 키 HKEY_CURRENT_USER 편집이 포함됩니다. 이 레지스트리는 사용자가 로그인하면 실행해야 하는 프로그램 목록으로 구성됩니다. 레지스트리 경로 -

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

아래는 Python 코드입니다.

# Python code to append or add current script to the registry
# module to modify or edit the windows registry
importwinreg as reg1
importos

defAddToRegistry() -

   # in python __file__ is denoeted as the instant of
   # file path where it was run or executed
   # so if it was executed from desktop,
   # then __file__ will be
   # c:\users\current_user\desktop
   pth1 =os.path.dirname(os.path.realpath(__file__))
   # Python file name with extension
   s_name1="mYscript.py"
   # The file name is joined to end of path address
   address1=os.join(pth1,s_name1)
   # key we want to modify or change is HKEY_CURRENT_USER
   # key value is Software\Microsoft\Windows\CurrentVersion\Run
   key1 =HKEY_CURRENT_USER
   key_value1 ="Software\Microsoft\Windows\CurrentVersion\Run"
   # open the key to make modifications or changes to
   open=reg1.OpenKey(key1,key_value1,0,reg1.KEY_ALL_ACCESS)
   # change or modifiy the opened key
   reg1.SetValueEx(open,"any_name",0,reg1.REG_SZ,address1)
   # now close the opened key
   reg1.CloseKey(open)
# Driver Code
if__name__=="__main__":
AddToRegistry()