1

I want to write results of some calculations to a file with print and print some other data like progress indication to Windows console so I can see it immediately.

What I write to console is: C:\Users\BoltKey>python program.py < in.txt > out.txt.

Is there a way to overload output redirection and print to console? Or is it just wrong and I should use file functions if I want to print to the console and into files at the same time?

Here is pseudocode:

for i in range(100): for j in range(100): result = getResult(i, j) print result // print to output file consolePrint "progress: " + str(i) + "%" // print to console 
0

2 Answers 2

2

You can print to stderr to achieve this (your command line is only redirecting stdout to a file). There are a couple of ways to achieve this, but a good one is found in this SO answer:

 import sys from __future__ import print_function 

...

 print("Message", file=sys.stderr) 
Sign up to request clarification or add additional context in comments.

Comments

1

Sounds an ugly hack to me.

Just use stderr for displaying progress messages.

1 Comment

Should add in an example rather than requiring a click.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.