FAQ
I'm trying to read a single keypress on Linux but expect to have the
programme running on Windows platform as well and find the mention in
the FAQ:

import termios, fcntl, sys, os
fd = sys.stdin.fileno()

oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)

oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

try:
while 1:
try:
c = sys.stdin.read(1)
print "Got character", `c`
except IOError: pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)

However this fails on the second line as sys.stdin seems to have no
method fileno.
Any idea how I might proceed?

Search Discussions

  • Mike Driscoll at Feb 25, 2008 at 7:14 pm

    On Feb 25, 12:35 pm, wyleu wrote:
    I'm trying to read a single keypress on Linux but expect to have the
    programme running on Windows platform as well and find the mention in
    the FAQ:

    import termios, fcntl, sys, os
    fd = sys.stdin.fileno()

    oldterm = termios.tcgetattr(fd)
    newattr = termios.tcgetattr(fd)
    newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
    termios.tcsetattr(fd, termios.TCSANOW, newattr)

    oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

    try:
    while 1:
    try:
    c = sys.stdin.read(1)
    print "Got character", `c`
    except IOError: pass
    finally:
    termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)

    However this fails on the second line as sys.stdin seems to have no
    method fileno.
    Any idea how I might proceed?
    I've never done this sort of thing (except in wxPython), but with a
    little Google-magic I found the following:

    A recipe that supposedly does this in a cross-platform way:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892

    And a Windows only module:
    http://effbot.org/librarybook/msvcrt.htm

    HTH

    Mike

    From http Mon Feb 25 20:25:34 2008
    From: http (Paul Rubin)
    Date: 25 Feb 2008 11:25:34 -0800
    Subject: PyEuler
    References: <9e435ab2-3323-4ad9-9dd9-cdde34c3fb91@e25g2000prg.googlegroups.com>
    Message-ID: <7x1w70x2hd.fsf@ruckus.brouhaha.com>

    bearophileHUGS at lycos.com writes:
    def takenth(n, iterable):
    "Returns the nth item"
    return list(islice(iterable, n, n+1))[0]
    return islice(iterable, n).next()
    isanymultiple = lambda x: any((x % y == 0) for y in nums)
    return sum(filter(isanymultiple, xrange(end)))
    This isn't so good, you really want to apply the filters recursively.
    def fibonacci(n):
    """Return nth element of the fibonacci serie"""
    if n == 0 or n == 1:
    return n
    return fibonacci(n-1) + fibonacci(n-2)
    uggh!!!! exponential blowup!
    def euler2(end):
    genfib = imap(fibonacci, count())
    Are you kidding?
    def ggenfib():
    a,b = 1,2
    while True:
    yield a
    a,b = b, a=b
    What I think about such code:
    - It's not much readable (but usually it can be read). ...
    Your observations are generally good; I'd say it was done
    without enough attention to the math too.

    There is a full set of solutions on the haskell wiki, if anyone cares.
  • Wyleu at Feb 25, 2008 at 8:52 pm
    A recipe that supposedly does this in a cross-platform way:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
    class _Getch:
    """Gets a single character from standard input. Does not echo to
    the
    screen."""
    def __init__(self):
    try:
    self.impl = _GetchWindows()
    except ImportError:
    self.impl = _GetchUnix()

    def __call__(self): return self.impl()


    class _GetchUnix:
    def __init__(self):
    import tty, sys

    def __call__(self):
    import sys, tty, termios
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
    tty.setraw(sys.stdin.fileno())
    ch = sys.stdin.read(1)
    finally:
    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch


    class _GetchWindows:
    def __init__(self):
    import msvcrt

    def __call__(self):
    import msvcrt
    return msvcrt.getch()


    getch = _Getch()



    Sadly this also fails with:


    Traceback (most recent call last):
    File "<pyshell#2>", line 1, in -toplevel-
    a = getch()
    File "/home/chris/getch.py", line 10, in __call__
    def __call__(self): return self.impl()
    File "/home/chris/getch.py", line 19, in __call__
    fd = sys.stdin.fileno()
    AttributeError: fileno


    What is fileno, and why might I not have it?
  • Wyleu at Feb 25, 2008 at 10:53 pm
    Aaah it doesn't work from idle but it does from the command line...
  • Rolf van de Krol at Feb 26, 2008 at 12:17 am

    wyleu wrote:
    Aaah it doesn't work from idle but it does from the command line...
    You are right. You can't read STDIN from IDLE. There has been a topic
    about that before:
    http://groups.google.com/group/comp.lang.python/browse_thread/thread/9f9c90cfe52378fe
  • Jeff Schwab at Feb 26, 2008 at 1:48 am

    Dennis Lee Bieber wrote:
    On Mon, 25 Feb 2008 10:35:54 -0800 (PST), wyleu
    <chris.lyon at spritenote.co.uk> declaimed the following in
    comp.lang.python:
    I'm trying to read a single keypress on Linux but expect to have the
    programme running on Windows platform as well and find the mention in
    the FAQ:
    UNIX terminal control stuff snipped.

    There is NO directly portable way to do low-level I/O with the
    console.
    What about curses?

    http://docs.python.org/lib/module-curses.html
    http://adamv.com/dev/python/curses/
  • Jeff Schwab at Feb 26, 2008 at 5:15 pm

    Dennis Lee Bieber wrote:
    On Mon, 25 Feb 2008 17:48:21 -0800, Jeff Schwab <jeff at schwabcenter.com>
    declaimed the following in comp.lang.python:
    I don't consider needing a 3rd party library for Windows, but not
    for UNIX/Linux a "portable" method...
    The Python module docs claim to support DOS without any kind of
    extension. I don't know how well (or whether) it works with new
    versions of Windows.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 25, '08 at 6:35p
activeFeb 26, '08 at 5:15p
posts7
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase