【问题标题】:How to start a python file at windows startup? [duplicate]如何在 Windows 启动时启动 python 文件? [复制]
【发布时间】:2021-01-22 02:38:08
【问题描述】:

建议一个简单的方法在启动时启动一个python文件。

【问题讨论】:

  • 如果您使用的是 windows,只需创建其快捷方式,然后将该文件添加到系统的启动文件夹中。

标签: python windows


【解决方案1】:

我找到了一个非常简单的方法。只需为您的脚本创建一个快捷方式文件并将快捷方式文件移动到 C:\Users\User_name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

仅此而已,我不知道为什么社区中的许多人都变得更难了。

【讨论】:

【解决方案2】:

第 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

【讨论】:

    猜你喜欢
    • 2011-05-25
    • 1970-01-01
    • 2011-08-28
    • 2022-08-15
    • 2011-07-12
    • 2015-11-22
    • 2023-03-29
    • 1970-01-01
    • 2015-01-31
    相关资源
    最近更新 更多