I have a python file and I am running the file.
If Windows is shutdown and booted up again, how I can run that file every time Windows starts?
Depending on what the script is doing, you may:
The actual solution depends on your needs, and what the script is actually doing.
Some notes on the differences:
As you can see, it all boils down to what you want to do; for instance, if it is something for your purposes only, I would simply drag it into startup folder.
In any case, lately I am leaning on solution #4, as the quickest and most straightforward approach.
if can simply add the following code to your script. Nevertheless, this only works on windows!:
import getpass
USER_NAME = getpass.getuser()
def add_to_startup(file_path=""):
if file_path == "":
file_path = os.path.dirname(os.path.realpath(__file__))
bat_path = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup' % USER_NAME
with open(bat_path + '\\' + "open.bat", "w+") as bat_file:
bat_file.write(r'start "" %s' % file_path)
this function will create a bat file in the startup folder that will run your script.
the file_path is the path to the file that you would like to run when your computer opens. you can leave it blank in order to add the running script to startup.
Create an exe file, I use pyinstaller "yourCode.py"
Add the execution file to your registry key: https://cmatskas.com/configure-a-runonce-task-on-windows/
Above mentioned all the methods did not worked I tried them all , I will tell you more simpler solution and alternative of windows task scheduler
Create a .bat file with content "ADDRESS OF YOUR PROJECT INTERPRETER" "ADDRESS OF YOUR PYTHON SCRIPT WITH SCRIPT NAME"
Store this bat file into the window startup folder(by default hidden) FYI: to find window startup folder press windos+r then type shell:startup -- it will directly take you to the startup folder
copy the bat file there with following 2 address in the same format , then simply restart the system or shut down and boot up.
The code will automatically run within 20 seconds of opening.
Thank me later