简体   繁体   English

在 Python 中暂停

[英]Pause in Python

I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.我通过使用实际脚本作为参数指向 Python 解释器的快捷方式,从 Windows 任务栏中运行命令行 Python 脚本。

After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.处理完脚本后,解释器终止并关闭输出窗口,这使得无法读取脚本输出。

What is the most straightforward way to keep the interpreter window open until any key is pressed?在按下任何键之前保持解释器窗口打开的最直接方法是什么?

In batch files, one can end the script with pause.在批处理文件中,可以暂停结束脚本。 The closest thing to this I found in python is raw_input() which is sub-optimal because it requires pressing the return key (instead of any key).我在 python 中发现的最接近的东西是raw_input() ,它是次优的,因为它需要按下返回键(而不是任何键)。

Any ideas?有任何想法吗?

一种方法是在最后留下一个raw_input()以便脚本在它终止之前等待您按 Enter 键。

Try os.system("pause") — I used it and it worked for me.试试os.system("pause") — 我用过它,它对我有用。

Make sure to include import os at the top of your script.确保在脚本顶部包含import os

There's no need to wait for input before closing, just change your command like so:在关闭之前无需等待输入,只需像这样更改您的命令:

cmd /K python <script>

The /K switch will execute the command that follows, but leave the command interpreter window open, in contrast to /C , which executes and then closes. /K开关将执行后面的命令,但保持命令解释器窗口打开,而/C则执行然后关闭。

The best option: os.system('pause') <-- this will actually display a message saying 'press any key to continue' whereas adding just raw_input('') will print no message, just the cursor will be available.最好的选择: os.system('pause') <-- 这实际上会显示一条消息,说“按任意键继续”,而只添加raw_input('')将不打印任何消息,只有光标可用。

not related to answer:与答案无关:

os.system("some cmd command") is a really great command as the command can execute any batch file/cmd commands. os.system("some cmd command")是一个非常棒的命令,因为该命令可以执行任何批处理文件/cmd 命令。

One way is to leave a raw_input() at the end so the script waits for you to press enter before it terminates.一种方法是在最后留下一个 raw_input() 以便脚本在它终止之前等待您按 Enter 键。

The advantage of using raw_input() instead of msvcrt.* stuff is that the former is a part of standard Python (ie absolutely cross-platform).使用 raw_input() 而不是 msvcrt.* 的好处是前者是标准 Python 的一部分(即绝对跨平台)。 This also means that the script window will be alive after double-clicking on the script file icon, without the need to do这也意味着双击脚本文件图标后,脚本窗口将处于活动状态,而无需执行

cmd /K python <script>

In Windows, you can use themsvcrt module.在 Windows 中,您可以使用msvcrt模块。

msvcrt.kbhit() Return true if a keypress is waiting to be read. msvcrt.kbhit()如果按键正在等待读取,则返回 true。

msvcrt.getch() Read a keypress and return the resulting character. msvcrt.getch()读取按键并返回结果字符。 Nothing is echoed to the console.控制台没有回显任何内容。 This call will block if a keypress is not already available, but will not wait for Enter to be pressed.如果按键不可用,此调用将阻塞,但不会等待按下 Enter。

If you want it to also work on Unix-like systems you can try this solution using the termios and fcntl modules.如果您希望它也适用于类 Unix 系统,您可以使用termiosfcntl模块尝试此解决方案

至于按什么键来关闭它的“问题”,我(和其他成千上万的人,我敢肯定)只是使用input("Press Enter to close")

There's a simple way to do this, you can use keyboard module's wait function.有一个简单的方法可以做到这一点,您可以使用键盘模块的wait功能。 For example, you can do:例如,您可以执行以下操作:

import keyboard
print("things before the pause")
keyboard.wait("esc") # esc is just an example, you can obviously put every key you want
print("things after the pause")

On Windows 10 insert at beggining this:Windows 10在开始时插入:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

Strange, but it works for me!奇怪,但它对我有用! (Together with input() at the end, of course) (当然还有最后的input()

If you type如果你输入

input("")

It will wait for them to press any button then it will continue.它将等待他们按下任何按钮,然后它会继续。 Also you can put text between the quotes.您也可以在引号之间放置文本。

Getting python to read a single character from the terminal in an unbuffered manner is a little bit tricky, but here's a recipe that'll do it:让 python 以无缓冲的方式从终端读取单个字符有点棘手,但这里有一个方法可以做到:

Recipe 134892: getch()-like unbuffered character reading from stdin on both Windows and Unix (Python) 配方 134892:在 Windows 和 Unix (Python) 上从标准输入读取类似 getch() 的无缓冲字符

An external WConio module can help here: http://newcenturycomputers.net/projects/wconio.html外部 WConio 模块可以在此处提供帮助: http ://new Centurycomputers.net/projects/wconio.html

import WConio
WConio.getch()
import pdb
pdb.debug()

This is used to debug the script.这用于调试脚本。 Should be useful to break also.破也应该有用。

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

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