Alex Rivera | Logout

How to pause a script when it ends on Windows?

Asked 2009-02-23T12:20:26.820
57

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.

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

Edit
Report

2 Answers

5

On Windows you can use the msvcrt module.

  • msvcrt.kbhit()
    Return True if a keypress is waiting to be read.

  • msvcrt.getch()
    Read a keypress and return the resulting character as a byte string. 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. If the pressed key was a special function key, this will return '\000' or '\xe0'; the next call will return the keycode. The Control-C keypress cannot be read with this function.

If you want it to also work on Unix-like systems you can try this solution using the termios and fcntl modules.

answered 2009-02-23T12:28:33.133
0
import pdb
pdb.debug()

This is used to debug the script. Should be useful to break also.

answered 2009-02-23T18:35:12.223

Your Answer