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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:31:20+00:00 2026-05-20T15:31:20+00:00

I have seen this question answered in reference to Bash, but can’t find one

  • 0

I have seen this question answered in reference to Bash, but can’t find one for Python. Apologies if this is repeating something.

Is it possible to print to the terminal and an output file with one command? I’m familiar with using print >> and sys.stdout = WritableObject, but I’d like to avoid having to double print commands for each line I want logged.

I’m using Python 2.6, just in case such knowledge is necessary.

More importantly, I want this to run on a Windows-based system using IDLE’s command line. So, in essence, I want the python script to report to IDLE’s terminal and a given log file.

EDIT: For anyone who finds this and decides to go with the answer I chose, if you need help understanding context managers (like I did), I recommend Doug Hellman’s Python Modules of the Week for clarification. This one details the context library. For help with decorators see this Stack Overflow question’s answers.

  • 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-20T15:31:20+00:00Added an answer on May 20, 2026 at 3:31 pm

    Replace sys.stdout.

    class PrintAndLog(object):
        def __init__(self, fileOrPath): # choose which makes more sense
            self._file = ...
    
        def write(s):
            sys.stdout.write(s)
            self._file.write(s)
    
        def close(self):
            self._file.close()
        # insert wrappers for .flush, .writelines
    
    _old_stdout = sys.stdout
    sys.stdout = PrintAndLog(f)
    ... # print and stuff
    sys.stdout = _old_stdout
    

    Can be put into a context manager (this is at least the third time I see something like this on SO alone…):

    from contextlib import contextmanager
    
    @contextmanager
    def replace_stdout(f):
        old_stdout = sys.stdout
        try:
            sys.stdout = PrintAndLog(f)
            yield
        finally:
            sys.stdout = old_stdout
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now I have seen this question in another forum but it didn't had an
I have seen this macro defined before but never really knew its purpose. Can
I have seen this question: Are there any decent UI components for touch screen
Haven't seen many Geneva related questions yet, I have posted this question in the
I have seen this syntax in MSDN: yield break , but I don't know
I am pretty sure I have seen this before, but I haven't found out
I have seen this problem arise in many different circumstances and would like to
I have seen this link: Implementing Mutual Exclusion in JavaScript . On the other
I have seen this in a lot of XML comments for classes in the
I have seen this mentioned a few times and I am not clear on

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.