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.
Afte
Yes, I know I'm posting to an old thread but I'd like to add my vote for using input() (3.x and up).
Added to the end of my .py script, it indeed prevents the console window from opening and immediately closing.
As to the "problem" of what key to press to close it, I (and 1,000's of others, I'm sure) simply use input("Press Enter to close").
Thanks for letting me add my 2 cents worth :)
.
Try os.system("pause")
— I used it and it worked for me.
Make sure to include import os
at the top of your script.
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.
There's a simple way to do this, you can use keyboard module's wait
function. 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")
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:
Recipe 134892: getch()-like unbuffered character reading from stdin on both Windows and Unix (Python)
One way is to leave a raw_input()
at the end so the script waits for you to press Enter before it terminates.