简体   繁体   English

如何让 Python 程序从终端连续运行(直到我手动停止它)?

[英]How can I make a Python program run continuously from the Terminal (until I stop it manually)?

I have a python program which takes an input (a single character, 'y' or 'n') from the user and then executes a certain task based on that input.我有一个 python 程序,它从用户那里获取输入(单个字符,'y' 或 'n'),然后根据该输入执行某个任务。 My need is to allow this program to run continuously from the terminal until I decide to stop it.我的需要是允许这个程序从终端连续运行,直到我决定停止它。 Currently, I have to keep going back to the terminal and execute the program from there (and always type in that single character).目前,我必须继续返回终端并从那里执行程序(并始终输入该单个字符)。

PS: If it helps: the program adds data to a MySQL database, so I need this in order to make the whole process automated (and thus a bit quicker) PS:如果有帮助:该程序将数据添加到MySQL数据库,所以我需要它以使整个过程自动化(从而更快一点)

EDIT编辑

My my-program.py looks like this:我的my-program.py看起来像这样:

main():
    if input().lower()=='y':
        #does something here
    else:
        #does something else

My requirement was to run a Python program infinitely from the Terminal.我的要求是从终端无限地运行 Python 程序。 I do know how to use loops and how to perform tasks based on user input.我知道如何使用循环以及如何根据用户输入执行任务。 What I wanted was to automatically give 'n' as the input character input whenever prompted.我想要的是在出现提示时自动将 'n' 作为输入字符输入。

my-program.py performs a certain operation when a character is given as input. my-program.py在输入字符时执行特定操作。 When i call my-program.main() from another Python program using a while loop as below, I want to keep passing the same input (say 'n' ) whenever prompted (when the input() statement of my-program.py is executed)当我使用如下所示的 while 循环从另一个 Python 程序调用my-program.main()时,我想在出现提示时继续传递相同的输入(比如'n' )(当my-program.pyinput()语句被执行)

import my-program
while True:
    my-program.main()

First install keyboard package:首先安装键盘包:

pip3 install keyboard

Then write the code:然后编写代码:

import keyboard  # using module keyboard
while True:  # making a loop
    try:  # used try so that if user pressed other than the given key error will not be shown
        if keyboard.is_pressed('q'):  # if key 'q' is pressed 
            print('You Pressed A Key!')
            break  # finishing the loop
        else:
            pass
    except:
        break  # if user pressed a key other than the given key the loop will break

Soruce: Keyboard input detection来源: 键盘输入检测

General case: the 'yes' command, it would use 'y' as your default option and won't ask you over and over again.一般情况:'yes' 命令,它会使用 'y' 作为您的默认选项,并且不会一遍又一遍地询问您。

$man yes $man 是的

yes - output a string repeatedly until killed [...]是 - 重复输出一个字符串直到被杀死 [...]

DESCRIPTION: Repeatedly output a line with all specified STRING(s), or 'y'.描述:重复输出包含所有指定 STRING(s) 或 'y' 的行。 It is probably equivalent to --force-yes and thus dangerous.它可能相当于 --force-yes ,因此很危险。 If you still want to do it, you pipe the output of yes:如果你仍然想这样做,你可以通过管道输出 yes:

yes | <command>

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

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