How to Autorun a Python Script On Windows Startup Using Windows Registry Settings Full Project For Beginners

How to Autorun a Python Script On Windows Startup Using Windows Registry Settings Full Project For Beginners

 

Welcome folks today in this blog post we will be discussing how to autorun a python script on windows startup using registry settings. All the source code of application will be given below.

 

 

 

Get Started

 

 

The Registry Path is located on your computer at:

 

`

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

 

 

Registry Path is located at :

 

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

 

In order to get started we need to make a app.py file and copy paste the following code

 

 

app.py

 

 

# Python code to add current script to the registry 

# module to edit the windows registry 
import winreg as reg 
import os            

def AddToRegistry(): 

    # in python __file__ is the instant of 
    # file path where it was executed 
    # so if it was executed from desktop, 
    # then __file__ will be 
    # c:\users\current_user\desktop 
    pth = os.path.dirname(os.path.realpath(__file__)) 
    
    # name of the python file with extension 
    s_name="mYscript.py"    
    
    # joins the file name to end of path address 
    address=os.join(pth,s_name) 
    
    # key we want to change is HKEY_CURRENT_USER 
    # key value is Software\Microsoft\Windows\CurrentVersion\Run 
    key = HKEY_CURRENT_USER 
    key_value = "Software\Microsoft\Windows\CurrentVersion\Run"
    
    # open the key to make changes to 
    open = reg.OpenKey(key,key_value,0,reg.KEY_ALL_ACCESS) 
    
    # modifiy the opened key 
    reg.SetValueEx(open,"any_name",0,reg.REG_SZ,address) 
    
    # now close the opened key 
    reg.CloseKey(open) 

# Driver Code 
if __name__=="__main__": 
    AddToRegistry()

 

See also  Python 3 Tkinter DOS Pacman Doodle Using PyGame Library GUI Script Desktop App Full Project For Beginners

 

 

 

 

 

Leave a Reply