Suggest a easy method to start a python file at startup.
-
1Just create its shortcut and than add that file in the startup folder of your system if you are using windows. – code by Abhishek Bharti Jan 21 '21 at 18:26
2 Answers
I found a very easy method. Just create a shortcut file for your script and move the shortcut file into C:\Users\User_name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
that's all I don't know why many people in the community are making it harder.
- 21
- 4
-
1... which would be even simpler if you've used search and find similar question with [identical answer](https://stackoverflow.com/a/1154771/10824407). – Olvin Roght Jan 21 '21 at 18:25
-
Who in the community made it harder? This is basically your exact answer https://stackoverflow.com/a/49193573/8678978 – chrisbyte Jan 21 '21 at 18:26
Step #1: Appending or Adding script to windows Startup folder:
After booting up of the windows, it executes (equivalent to double-clicking) all the application present in its startup folder or directory. Address
C:\Users\current_user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
Step #2: Appending or adding script to windows Registry:
This process may be risky if not accomplished properly, it includes editing the windows registry key HKEY_CURRENT_USER from the python script itself. This registry consists of the list of programs that must execute once the user Login.
Python code below:
# 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()
from here
- 126
- 7