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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:18:04+00:00 2026-05-24T17:18:04+00:00

I thought the print statement just called the .write() method on the sys.stdout (by

  • 0

I thought the print statement just called the .write() method on the sys.stdout (by default) object.

but having written a subclass like this:

import time

class logfile(file):
    def __init__(self, *args, **kwargs):
        file.__init__(self, *args, **kwargs)

    def write(self, logstr):
        if logstr[-1] != '\n': logstr += '\n'
        super(logfile, self).write(time.strftime('%D-%T ') + str(logstr))

It seems to work if I create a logfile object and call the write method, but when trying to change the sys.stdout object to an instance of the logfile it appears as though print isn’t calling write. Maybe writelines?

Using this:

#!/usr/bin/python

from myfile import logfile
import sys

sys.stdout = logfile('somefile', 'w')

print 'this is a test'
sys.stdout.write('this is another test')

My output file ‘somefile’ contains:

this is a test
08/10/11-16:59:47 this is another test

You can see the first line in the output file is what I tried to print and the second line is what was used in sys.stdout.write

I thought print just called the write method- clearly I’m missing something basic.

  • 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-24T17:18:04+00:00Added an answer on May 24, 2026 at 5:18 pm

    Apparently this is an limitation of the implementation of Python 2 where print is a statement rather than an expression with side-effects (as it is in Python 3).

    I rewrote the code to something that works in Python 3:

    from io import FileIO
    import time
    
    class logfile(FileIO):
      def __init__(self, *args, **kwargs):
        FileIO.__init__(self, *args, **kwargs)
    
      def write(self, logstr):
        if logstr[-1] == '\n': logstr = logstr[:-1]
        super(logfile, self).write(bytes(time.strftime('%D-%T ') + str(logstr), 'UTF-8'))
    
    import sys
    
    sys.stdout = logfile('somefile', 'w')
    
    print("This is a test")
    sys.stdout.write('this is another test')
    

    As far as I know there is no way to create the same behaviour in Python 2.

    I also tried using from __future__ import print_function but that made no difference.

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

Sidebar

Related Questions

I thought I understood what the default method does to a hash... Give a
I just tried adding some print statements to my shouldautorotate method and noticed that
I thought I heard that py2exe was able to do this, but I never
I thought people would be working on little code projects together, but I don't
I thought I understood Java generics pretty well, but then I came across the
I'm having trouble with a line inside an if statement executing before the conditional
I'd like to write the following as a MySQL SELECT statement to cut down
This is going to seem extremely trivial, but it just doesn't make any sense
Probably just a gross oversight of some sort, but I'm not receiving any WM_SIZE
I'm having trouble understanding the for/in statement in JavaScript. The book which I'm using

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.