简体   繁体   English

如何在 windows 打开时自动运行 python 文件

[英]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.当日期更正并更改第二个文件时。

Open your startup folder by pressing Win + R and typing in shell:startup按 Win + R 并输入 shell:startup 打开您的启动文件夹

Within this folder, create a txt file and rename it to anything.bat and edit it with在此文件夹中,创建一个 txt 文件并将其重命名为anything.bat 并使用编辑它

@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.如果您希望它有一个终端 window 弹出窗口,您可以删除“@ECHO OFF”。

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在你打开 dosya.txt 的任何地方都这样做,就像在 dosya.txt 写在下面

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.如果从您计算机上的任何目录运行,设置它的完整路径将使脚本工作。

I have tried this way for.bat files, and it worked you can also try.我已经为 .bat 文件尝试过这种方式,并且它有效,您也可以尝试。 Put your python files that you want to run on windows startup at this location.将要在 windows 启动时运行的 python 文件放在此位置。 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

I have written a script that can auto-run any script when PC starts我写了一个脚本,可以在 PC 启动时自动运行任何脚本

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如果您使用的是 python 2.7.x,则将winreg替换为_winreg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM