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 8870003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:36:56+00:00 2026-06-14T17:36:56+00:00

I have a program depending on a large code base that prints a lot

  • 0

I have a program depending on a large code base that prints a lot of irrelevant and annoying messages. I would like to clean them up a bit, but since their content is dynamically generated, I can’t just grep for them.

Is there a way to place a hook on the print statement? (I use python 2.4, but I would be interested in results for any version). Is there another way to find from which “print” statement the output comes?

  • 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-14T17:36:57+00:00Added an answer on June 14, 2026 at 5:36 pm

    For CPython2.5 or older:

    import sys
    import inspect
    import collections
    _stdout = sys.stdout
    
    Record = collections.namedtuple(
        'Record',
        'frame filename line_number function_name lines index')
    
    class MyStream(object):
        def __init__(self, target):
            self.target = target
        def write(self, text):
            if text.strip():
                record = Record(*inspect.getouterframes(inspect.currentframe())[1])        
                self.target.write(
                    '{f} {n}: '.format(f = record.filename, n = record.line_number))
            self.target.write(text)
    
    sys.stdout = MyStream(sys.stdout)
    
    def foo():
        print('Hi')
    
    foo()
    

    yields

    /home/unutbu/pybin/test.py 20: Hi
    

    For CPython2.6+ we can import the print function with

    from __future__ import print_function
    

    and then redirect it as we wish:

    from __future__ import print_function
    import sys
    import inspect
    import collections
    
    Record = collections.namedtuple(
        'Record',
        'frame filename line_number function_name lines index')
    
    def myprint(text):
        if text.strip():
            record = Record(*inspect.getouterframes(inspect.currentframe())[1])        
            sys.stdout.write('{f} {n}: '.format(f = record.filename, n = record.line_number))
        sys.stdout.write(text + '\n')
    
    def foo():
        print('Hi')
    
    print = myprint
    foo()
    

    Note that inspect.currentframe uses sys._getframe which is not part of all implementations of Python. So the solution above may only work for CPython.

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

Sidebar

Related Questions

I have this program that at one point accesses os.getcwd(), but some times, depending
I have a program that creates UIViewControllers in a UISplitView depending upon which row
I have a program that will need to run different methods depending on what
I have a program that needs two different values of the same variable depending
Depending on the amount of input data, I have a program that runs in
I have program that requires Python 3, but I develop Django and it uses
I have program that has a variable that should never change. However, somehow, it
I have program, that must interact with a console program before my program can
I have program that runs fast enough. I want to see the number of
I have a program that gets a JSON from the server using getJSON and

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.