Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8455299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:15:07+00:00 2026-06-10T12:15:07+00:00

I am trying to figure out how to create a little Python script that

  • 0

I am trying to figure out how to create a little Python script that can take the following parameters:

  1. prompt – string
  2. time to wait as an integer
  3. number of characters before stopping

The last one is the number of characters I can enter before the program stops accepting characters and starts processing the input. I’ve seen some people use Python’s select.select method, but that doesn’t account for the 3rd item. I’m leaning towards curses, although I don’t know if it supports a timeout which is making me think of threads. Any insights would be great! This will run on Linux with Python 2.6.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:15:08+00:00Added an answer on June 10, 2026 at 12:15 pm

    Ok, I have achieved it :D.

    #!/usr/bin/env python
    
    import sys
    from select import select
    
    def main(argv):
        timeout = 3
        prompt = '> '
        max_chars = 3
    
        # set raw input mode if relevant
        # it is necessary to make stdin not wait for enter
        try:
            import tty, termios
    
            prev_flags = termios.tcgetattr(sys.stdin.fileno())
            tty.setraw(sys.stdin.fileno())
        except ImportError:
            prev_flags = None
    
        buf = ''
        sys.stderr.write(prompt)
    
        while True: # main loop
            rl, wl, xl = select([sys.stdin], [], [], timeout)
            if rl: # some input
                c = sys.stdin.read(1)
                # you will probably want to add some special key support
                # for example stop on enter:
                if c == '\n':
                    break
    
                buf += c
                # auto-output is disabled as well, so you need to print it
                sys.stderr.write(c)
    
                # stop if N characters
                if len(buf) >= max_chars:
                    break
            else:
                # timeout
                break
    
        # restore non-raw input
        if prev_flags is not None:
            termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, prev_flags)
        # and print newline
        sys.stderr.write('\n')
    
        # now buf contains your input
        # ...
    
    if __name__ == "__main__":
        main(sys.argv[1:])
    

    It’s fairly incomplete; I just put a few values to test it. A few words of explanation:

    1. You need to switch the tty to ‘raw’ mode — otherwise you wouldn’t be able to get input without it being confirmed by enter key,
    2. in raw mode the typed in characters are no longer output by default — you need to output them yourself if you want user to see what he is typing,
    3. you probably want to handle special keys like enter and backspace — I’ve added enter handling here. Maybe you could reuse parts of curses for that,
    4. I’ve assumed the timeout is ‘3 seconds after last key’. If you want timeout for whole process, I think the easiest way would be to get current time, increase it by timeout (i.e. get end_time), and then pass end_time - current_time in seconds as timeout to select(),
    5. I’ve made unix-specific imports optional. I don’t know whether it will work on Windows correctly, though.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out how to create a temporary table that is
I am very new to Python, and trying to figure out how to create
I'm trying to create a little free-hand drawing app, and to figure out a
A little advise please. I am trying to create a tree out of following
I am trying to figure out how to create an image gallery like the
I'm trying to figure out how to create a http CRAN-repository. I've tried to
i am trying to figure out how to create a javascript alert box asking
I am trying to figure out how to create 3 divs and have them
I'm trying to figure out how to create personalized urls for double-byte languages. For
I'm trying to figure out the best way to create a row of three

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.