Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.