0

I want my python file to run if Windows is booted up every single time, so I used this simple code: Background.py (This creates a .bat file which will run my desired .py file on every windows startup)

import getpass
import os
USER_NAME = getpass.getuser()

def add_to_startup():
    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'C:\Users\intel\AppData\Local\Programs\Python\Python38\python.exe C:\Users\intel\Desktop\Python programs\testing.py %*')

add_to_startup()


And this is the .py file which I want to run every time:

with open(r"C:\Users\intel\Desktop\hello.txt", "w+") as test_file:
    test_file.write(r'start')

But it's not working even if my computer is on :(

Edit 1: I checked The startup folder also and the open.bat file is successfully created and existing... (You can even check in the image) check the image enter code here

  • Did you had a look there: https://superuser.com/questions/954950/run-a-script-on-start-up-on-windows-10 – Maurice Meyer May 28 '20 at 15:01
  • Yes. And the file is already created... –  May 28 '20 at 15:03
  • Personally, i prefer the 'Scheduled Tasks' method :) – Maurice Meyer May 28 '20 at 15:04
  • Haha no, I want the every startup one :) –  May 28 '20 at 15:06
  • 1
    Read the answers again, the 'Scheduled Tasks' method is executing your script at startup , too. It's way easier to debug (and you can trigger manually) for testing. – Maurice Meyer May 28 '20 at 15:09
  • sidenote: have a look at [pathlib](https://docs.python.org/3/library/pathlib.html) to avoid haveing to use raw string, double backslashes and the like. – FObersteiner May 28 '20 at 15:20
  • @Maurice-Meyer Is there a way to do that through python? I mean I want the program to do this by itself without any manual clicks... –  May 28 '20 at 15:58
  • @Roxanne, you could try this: https://stackoverflow.com/questions/26160900/is-there-a-way-to-add-a-task-to-the-windows-task-scheduler-via-python-3. – Maurice Meyer May 28 '20 at 19:14

0 Answers0