Deneme
Deneme

Reputation: 13

How to make auto-run python file when windows open

from datetime import date

bugun = str(date.today())

if bugun == "2021-04-25":
    with open("dosya.py","r+") as dosya:
        liste = dosya.readlines()
        liste.insert(3,"DenemeBu\n")
        del liste[4]
        dosya.seek(0)
        print(liste)
        with open("dosya.py","w") as dosya:
            for i in liste:
                dosya.write(i)
import os
print("Hello")
sayi1 = int(input("Sayi1: "))
sayi2 = int(input("Sayi2: "))
print("Sonuc {}".format(sayi1+sayi2))

I want to change second file with first file but I want first file to open when my pc opens and takes current date. When date corrects and changes second file.

Upvotes: 0

Views: 3035

Answers (3)

pudup
pudup

Reputation: 131

Open your startup folder by pressing Win + R and typing in shell:startup

Within this folder, create a txt file and rename it to anything.bat and edit it with

@ECHO OFF
python3 C:/path/to/my/script.py

You can remove the "@ECHO OFF" if you want it to have a terminal window popup.

EDIT: As for the error you're facing. Change

open("dosya.txt", "r")

to

open("C:/full/path/to/dosya.txt", "r")

Do that everywhere you open dosya.txt, like at the dosya.txt write below

You're facing this error because you're running the command for the script from a directory that doesn't contain that file and it tries to find that file in it's running directory and can't find it. Setting the full path to it will make the script work if run from any directory on your computer.

Upvotes: 1

Ghanteyyy
Ghanteyyy

Reputation: 494

I have written a script that can auto-run any script when PC starts

import os
import winreg


class is_at_startup:
    '''Adding the given program path to startup'''

    def __init__(self, program_path):
        self.program_path = program_path
        self.program_basename = os.path.basename(self.program_path)

    def main(self):
        '''Adding to startup'''

        if os.path.exists(self.program_path):
            areg = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)

            try:
                akey = winreg.OpenKey(areg, f'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\{self.program_basename}', 0, winreg.KEY_WRITE)
                areg.Close()
                akey.Close()

                print(f'{self.program_path} already at startup')

            except WindowsError:
                key = winreg.OpenKey(areg, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run', 0, winreg.KEY_SET_VALUE)
                winreg.SetValueEx(key, f'{self.program_basename}', 0, winreg.REG_SZ, f'{self.program_basename}')

                areg.Close()
                key.Close()

                print(f'{self.program_path} added to startup')


if __name__ == '__main__':
    startup = is_at_startup('your program path')
    startup.main()

If you are using python 2.7.x then replace winreg with _winreg

Upvotes: 1

Awais Khan
Awais Khan

Reputation: 37

I have tried this way for .bat files, and it worked you can also try. Put your python files that you want to run on windows startup at this location. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

Upvotes: 0

Related Questions