Search code examples
pythonregeditautorun

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.


Solution

  • 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.