简体   繁体   English

如何在 Python 脚本中暂停并等待命令输入

[英]How to pause and wait for command input in a Python script

Is it possible to have a script like the following in Python?是否可以在 Python 中使用如下脚本?

...
Pause
->
Wait for the user to execute some commands in the terminal (e.g.
  to print the value of a variable, to import a library, or whatever).
The script will keep waiting if the user does not input anything.
->
Continue execution of the remaining part of the script

Essentially the script gives the control to the Python command line interpreter temporarily, and resume after the user somehow finishes that part.本质上,脚本暂时将控制权交给 Python 命令行解释器,并在用户以某种方式完成该部分后恢复。


What I come up with (inspired by the answer) is something like the following:我想出的(受答案启发)类似于以下内容:

x = 1

i_cmd = 1
while True:
  s = raw_input('Input [{0:d}] '.format(i_cmd))
  i_cmd += 1
  n = len(s)
  if n > 0 and s.lower() == 'break'[0:n]:
    break
  exec(s)

print 'x = ', x
print 'I am out of the loop.'

if you are using Python 2.x: raw_input()如果您使用的是 Python 2.x: raw_input()

Python 3.x: input() Python 3.x: input()

Example:例子:

# Do some stuff in script
variable = raw_input('input something!: ')
# Do stuff with variable

The best way I know to do this is to use the pdb debugger.我所知道的最好的方法是使用 pdb 调试器。 So put所以放

import pdb

at the top of your program, and then use在程序的顶部,然后使用

pdb.set_trace()

for your "pause".为了你的“暂停”。

At the (Pdb) prompt you can enter commands such as在 (Pdb) 提示符下,您可以输入命令,例如

(Pdb) print 'x = ', x

and you can also step through code, though that's not your goal here.您还可以逐步执行代码,尽管这不是您的目标。 When you are done simply type完成后只需键入

(Pdb) c 

or any subset of the word 'continue', and the code will resume execution.或“继续”一词的任何子集,代码将继续执行。

A nice easy introduction to the debugger as of Nov 2015 is at Debugging in Python , but there are of course many such sources if you google 'python debugger' or 'python pdb'.截至 2015 年 11 月, 调试器的一个很好的简单介绍是在 Python调试,但是如果您在 google 上搜索“python 调试器”或“python pdb”,当然会有很多这样的来源。

I think you are looking for something like this:我想你正在寻找这样的东西:

import re

# Get user's name
name = raw_input("Please enter name: ")

# While name has incorrect characters
while re.search('[^a-zA-Z\n]',name):

    # Print out an error
    print("illegal name - Please use only letters")

    # Ask for the name again (if it's incorrect, while loop starts again)
    name = raw_input("Please enter name: ")

Waiting for user input to 'proceed':等待用户输入“继续”:

The input function will indeed stop execution of the script until a user does something.输入函数确实会停止脚本的执行,直到用户做某事。 Here's an example showing how execution may be manually continued after reviewing pre-determined variables of interest:这是一个示例,显示在查看预先确定的感兴趣变量后如何手动继续执行:

var1 = "Interesting value to see"
print("My variable of interest is {}".format(var1))
key_pressed = input('Press ENTER to continue: ')

Proceed after waiting pre-defined time:等待预定义时间后继续:

Another case I find to be helpful is to put in a delay, so that I can read previous outputs and decide to Ctrl + C if I want the script to terminate at a nice point, but continue if I do nothing.我发现另一个有用的情况是延迟,以便我可以读取以前的输出并决定Ctrl + C如果我希望脚本在一个好的点终止,但如果我什么都不做就继续。

import time.sleep
var2 = "Some value I want to see"
print("My variable of interest is {}".format(var2))
print("Sleeping for 5 seconds")
time.sleep(5) # Delay for 5 seconds

Actual Debugger for executable Command Line:可执行命令行的实际调试器:

Please see answers above on using pdb for stepping through code请参阅上面有关使用pdb步执行代码的答案

Reference: Python's time.sleep() – Pause, Stop, Wait or Sleep your Python Code参考: Python 的 time.sleep()——暂停、停止、等待或睡眠你的 Python 代码

Simply use the input() function as follows:只需按如下方式使用 input() 函数:

# Code to be run before pause
input() # Waits for user to type any character and
        # press Enter or just press Enter twice

# Code to be run after pause

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

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