-1

I am trying to redirect of print statement of reporterror function into file but output of print statement did not redirect into text file May I know what am i missing here Please note I have created one live scenario here and I can not modify report error function as it's in framework and modificaition will require lot of testing

import os

def reporterror():
    print ("file not found")
    errors = ["error", "error1", "error3"]
    for er in errors:
        print (er)
    return 1
    
def info():
    print (os.getcwd())
    with open("info.txt", "w") as finfo:
        print(reporterror(), file=finfo)
   
info()

Output in .txt file:

1

Expected output:

error
error1
error2

gmdev
  • 2,725
  • 2
  • 13
  • 28
user765443
  • 1,856
  • 7
  • 31
  • 56
  • `reporterror()` doesn't return anything, so it doesn't make sense to print its output. If you want to redirect STDOUT to a file, you can do that through the command line. – cs95 Oct 26 '20 at 11:29
  • f you can't change `reporterror`, you will have to change `sys.stdout` as this is the default for `print`. In that case... – Tomerikoo Oct 26 '20 at 11:34
  • Does this answer your question? [Redirect stdout to a file in Python?](https://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python) – Tomerikoo Oct 26 '20 at 11:34
  • I just create a scenario here and need to invoke function here – user765443 Oct 26 '20 at 11:34
  • it is redirect out of file But I need to invoke function Thx I have already gone through link – user765443 Oct 26 '20 at 11:35
  • your comments are more confusing than your question. What do you want to happen to the print statements in reporterrror? Maybe you need to monkey-patch or decorate reporterror – rioV8 Oct 26 '20 at 11:38

3 Answers3

1

Someone has already shared the link to the top answer on redirecting stdout but you haven't picked up on the part in that answer that might help.

from contextlib import redirect_stdout

with open('log.txt', 'w') as f:
    with redirect_stdout(f):
        my_function() #Call your function here any prints will go to the file
        print("This text") #This will be output to the file too

This will temporarily redirect the output of print to the log.txt file.

scotty3785
  • 6,763
  • 1
  • 25
  • 35
0

You expect the print statements that are inside reporterror() to also write to the file. However, this is not the case. The only thing that happens here is that reporterror() returns 1 and prints each value in errors. The print statements in reporterror() do not just inherit the file from the print statement in info(). Below, I return a string that contains all of the values in errors followed by a line break. The print statement inside of info() will now print the output of reporterror().

import os

def reporterror():
    errors = ["error", "error1", "error3"] 
    return "\n".join(errors)

def info():
    print (os.getcwd())
    with open("info.txt", "w") as finfo:
        print(reporterror(), file=finfo)

info()

If you want to capture all output, regardless of whether or not the print statements specify an output file, you can redirect the scripts output into a .txt file through the terminal:

python script.py > info.txt

It should be worth noting that unless you have a valid reason for using print() to write to a file, it is really better off to just use:

file.write(contents)
gmdev
  • 2,725
  • 2
  • 13
  • 28
0

if you want all the print statements (AKA standard output stdout) output to be redirect to a specific file just use this snippet

import sys
sys.stdout = open('<<<PATH_TO_YOUR_FILE>>>', 'w')
print('test')

this shall do it.

Radwan Abu-Odeh
  • 1,897
  • 9
  • 16