将Python脚本添加到Windows启动时,基本上表示Python脚本将在Windows启动时运行。这可以通过两步过程来完成-
步骤#1:在Windows启动文件夹中添加或添加脚本
在启动Windows后,它会执行(相当于双击)其启动文件夹或目录中存在的所有应用程序。
C:\Users\current_user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
默认情况下,current_user下的AppData目录或文件夹是隐藏的,使隐藏文件能够获取它,并将脚本的快捷方式粘贴到给定的地址或脚本本身中。除此之外,必须将.PY文件默认设置为python IDE,否则脚本可能最终以文本形式打开而不是执行。
步骤2:向Windows注册表添加或添加脚本
如果未正确完成,则此过程可能会很冒险,它包括从python脚本本身编辑Windows注册表项HKEY_CURRENT_USER。该注册表包含用户登录后必须执行的程序列表。注册表路径-
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
# Python code to append or add current script to the registry # module to modify or edit the windows registry importwinreg as reg1 importos
# 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()