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

In Microsoft SQL Server: I have seen this question several times, but can't see
I have seen this question already answered here but when I tried the same
I think I have seen this question before but I don't think it's answered
I have seen this question answered here, but unfortunately the answer does not work.
I've seen this question asked here but I haven't found one that is answered
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
I have seen many answers for this type of question but its not related
This question seems to have been asked a lot, but I haven't seen an
I have seen different questions regarding this, but I still find this topic to
This is a pretty stale question but frankly I'm yet to find it answered

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.