1

I'm trying to create one behavior of the script on Linux/Mac/Windows machines with Python 2.7.x.

With a little help from this threads:

I can achieve desired results: by pressing any key script will exit.

But maybe there is a better way to do this? Can someone please help me?

Thanks!

#!/usr/bin/env python2.7

import os
import sys

def wait_for_press():
    if sys.platform == 'win32':
        os.system("pause")
    elif sys.platform in ('linux2', 'darwin'):
        import termios
        import tty

        print "Press any key to continue..."

        stdin_file_desc = sys.stdin.fileno()
        old_stdin_tty_attr = termios.tcgetattr(stdin_file_desc)
        try:
            tty.setraw(stdin_file_desc)
            sys.stdin.read(1)
        finally:
            termios.tcsetattr(stdin_file_desc, termios.TCSADRAIN, old_stdin_tty_attr)

if __name__ == "__main__":
    wait_for_press()
Community
  • 1
  • 1
silent
  • 155
  • 3
  • 14

1 Answers1

0

Yuck. Why do you have OS-specific code? What happens if someone is using 'linux3'? You're probably looking for the curses library.

Refer to nachik's answer on Python read a single character from the user

Community
  • 1
  • 1
IceArdor
  • 1,961
  • 19
  • 20