Skip to content

Instantly share code, notes, and snippets.

@davstott
Created March 5, 2013 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davstott/5086982 to your computer and use it in GitHub Desktop.
Save davstott/5086982 to your computer and use it in GitHub Desktop.
Linux python character mode stdin instead of line mode
import sys, fcntl, os, termios, tty
def init():
#take the current settings
fnctlSettings = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
termiosSettings = termios.tcgetattr(sys.stdin.fileno())
# set the new ones
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, os.O_NONBLOCK | fnctlSettings)
tty.setraw(sys.stdin.fileno())
return [fnctlSettings, termiosSettings]
if __name__ == '__main__':
originalSettings = init()
try:
#do Stuff
finally:
#put the original settings back, even if we've had an exception
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, originalSettings[0])
termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, originalSettings[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment