【发布时间】:2021-01-22 02:38:08
【问题描述】:
建议一个简单的方法在启动时启动一个python文件。
【问题讨论】:
-
如果您使用的是 windows,只需创建其快捷方式,然后将该文件添加到系统的启动文件夹中。
建议一个简单的方法在启动时启动一个python文件。
【问题讨论】:
我找到了一个非常简单的方法。只需为您的脚本创建一个快捷方式文件并将快捷方式文件移动到 C:\Users\User_name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
仅此而已,我不知道为什么社区中的许多人都变得更难了。
【讨论】:
第 1 步:将脚本附加或添加到 Windows 启动文件夹:
Windows 启动后,它会执行(相当于双击)其启动文件夹或目录中存在的所有应用程序。 地址
C:\Users\current_user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
步骤 #2:向 Windows 注册表附加或添加脚本:
如果没有正确完成这个过程可能会有风险,它包括从 python 脚本本身编辑 Windows 注册表项 HKEY_CURRENT_USER。此注册表包含用户登录后必须执行的程序列表。
Python 代码如下:
# 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()
来自here
【讨论】: