0

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
BoltKey
  • 1,994
  • 1
  • 14
  • 26

2 Answers2

1

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)
Community
  • 1
  • 1
Turn
  • 6,656
  • 32
  • 41
0

Sounds an ugly hack to me.

Just use stderr for displaying progress messages.

Community
  • 1
  • 1
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176