Pause in Python

后端 未结 13 1183
臣服心动
臣服心动 2020-12-24 10:59

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

相关标签:
13条回答
  • 2020-12-24 11:21

    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 :)

    .

    0 讨论(0)
  • 2020-12-24 11:23

    Try os.system("pause") — I used it and it worked for me.

    Make sure to include import os at the top of your script.

    0 讨论(0)
  • 2020-12-24 11:23

    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.

    0 讨论(0)
  • 2020-12-24 11:26

    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")
    
    0 讨论(0)
  • 2020-12-24 11:28

    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)

    0 讨论(0)
  • 2020-12-24 11:31

    One way is to leave a raw_input() at the end so the script waits for you to press Enter before it terminates.

    0 讨论(0)
提交回复
热议问题