1

Currently, I can print information to stdout and also to stderr, but I would like to print information about progress and print it to stdout, stderr and somewhere else. Is this possible?

3
  • 1
    Where do you want to print it? Commented Aug 26, 2012 at 16:21
  • Are you saying that you want to simulataneously print output to stdout, stderr, and somewhere else? Commented Aug 26, 2012 at 16:30
  • Yes to stdout, stderr and somewhere else - to the console. Commented Aug 26, 2012 at 16:35

3 Answers 3

3

You can use the logging module to log information simultaneously to different places:

import logging logger = logging.getLogger('your_logger') logger.setLevel(logging.INFO) file_handler = logging.FileHandler('errors.log') file_handler.setLevel(logging.ERROR) logger.addHandler(file_handler) stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.INFO) logger.addHandler(stream_handler) logger.warning('I am a warning') logger.error('I will be printed, but written to the log file as well') logger.info('I am an info message') 
Sign up to request clarification or add additional context in comments.

5 Comments

I am not that familiar with the logging module, isn't the output to the console (if there is any) delivered via stderr or stdout? (I need to read up on that module)
when I redirect stderr to the file, that redirect logger messages too
@Levon: I think that depends on the severity of the message.
@Andrew: Can you explain a bit more?
@Blender I guess my question is geared towards OP wanting to display messages to the console in addition to using stderr and stdout .. I don't think that's possible (ie stdout and stderr will always be involved for display to the console). OP can display simultaneously to a number of "places" (incl files), but only stderr/stdout go to term unless I'm mistaken.
1

Output to the console comes only via stdout and stderr as far as I know.

Do you want to print to a file, then you can do this

 with open('outfile.txt', 'w') as f: f.write('this is a test') 

or alternatively if you prefer using print:

 print >> f, 'this is a test' 

In the above example a file named outfile.txt is opened for write access. This means any data already present will be wiped out by new data written to it. If you wanted to append you'd open the file in a mode. See the documentation for open() for more information.

Files that are opened need to be closed, using the with construct takes care of that for you (and also closes the file in case you encounter an exception).

3 Comments

Cause I wanna print progress information - it should be print to the term.
@Andrew Output to the console comes only via stdout and stderr as far as I know. If not, then I'll be happy to learn something new.
printing directly to /dev/console on a unix-like machine will probably work, although it's not necessarily a good idea.
-1

Make a class with method named write(), then you can use

print 'text' >> Printer() 

to print 'text' to the Printer() object (python 2.x) or

print('text', file=Printer()) 

in python 3.x

2 Comments

any string you'd like to print, just replace 'text' with it
@pythonm: I would suggest subclassing StringIO (which already has a write method).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.