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

  • SEARCH
  • Home
  • 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 6749331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:40:40+00:00 2026-05-26T12:40:40+00:00

Setup I’ve been playing with a script to pipe tail -f into and highlight

  • 0

Setup

I’ve been playing with a script to pipe tail -f into and highlight certain keywords. Not a big project but something I find useful.

Right now the main loop is basically:

...
line = True
while line:
    line = sys.stdin.readline()
    sys.stdout.write(highlight(line))
...

I’d like to listen for certain keypresses during this loop to print a marker line in the log. The method I’ve found that looks like it would work for getting a keypress is described on http://code.activestate.com/recipes/134892/ but it reads a single character at a time from stdin which won’t work when my main loop is reading entire lines from it.

Question

Is there a way in Python to listen for keypresses while also reading piped input?

I have wrapped the main function in a try block which excepts KeyboardInterrupt and prints a nice little goodbye message instead of an error stack trace. Is there a way to piggyback on this behavior with another key?

I’d rather not use a beefy (compared to my small script) module like pygame or tkinter and be forced to use their main loop just to get access to keypresses. (also I’m not familiar with how either would behave when receiving piped input)

  • 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-05-26T12:40:41+00:00Added an answer on May 26, 2026 at 12:40 pm

    The simplest solution would be to avoid using a pipe from tail at all. Stick to using your stdin for user input, and instead write your tool so that it can open and follow the file you want to read from natively without using tail at all. tail’s not a terribly complex tool if you just want to reimplement that part of it.

    Alternately, you could create a named pipe which will allow you to pipe tail into it, and then your program can read it like a file and still have access to its own stdin.

    If neither of these are options, I think going with a separate library that directly polls the keyboard might be your best bet. For the record, pygame doesn’t actually have a “main loop”, it has an event pump function that you should call as often as possible (in your main loop) but if you don’t, it’s not going to die. It can buffer lots and lots of events between pumps, and you can set filters on it to discard all the events you don’t care about and only keep the ones you do (keypresses). The main problem with pygame would be that it’s a pretty heavyweight library to be requiring for such a simple tool, so I understand your hesitation to go that route.

    Oh, and as a final option (least desirable, in my opinion) you could abuse the keyboard interrupt mechanic to create “Emacs-like” key chords. Like, press Ctrl-C, then Ctrl-(whatever). The Ctrl-C invokes the KeyboardInterrupt, then in the handler for that you read the next character off stdin and do something with that. Once you’re done, return to your loop. But that’s ugly and hacky and I’m not even sure if it would work reliably.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Setup Have you ever had the experience of going into a piece of code
Setup is following: Drupal project, one svn repo with trunk/qa/production-ready branches, vhosts for every
Setup: IIS7 serving ASP classic VB script code which generates a dynamic VSC page/file
Setup project with Visual studio 2008 really sucks. Do you know if there is
Setup: I'm looking for insight into using a ABPersonViewContoller without getting data from the
Setup: Python script A inserts data to a database every 15 minutes Python script
I setup a Google App Engine project in Eclipse. I added a root level
Setup I'm writing a script to process and annotate build logs from Visual Studio.
setup A TurboGears2 project using ming as an ORM for mongodb. I'm used to
Setup I have a website that draws RSS feeds and displays them on the

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.