简体   繁体   English

重定向 function output 到文本文件

[英]Redirecting function output to text file

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我正在尝试将reporterror function 的打印语句重定向到文件中,但是 output 的打印语句没有重定向到文本文件中我可以知道我在这里缺少什么请注意我在这里创建了一个实时场景,我无法将报告错误 function 修改为它在框架中,修改将需要大量测试

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: Output in.txt文件:

1 1个

Expected output:预计 output:

error错误
error1错误1
error2错误2

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.有人已经分享了有关重定向 stdout 的最佳答案的链接,但您还没有找到该答案中可能有帮助的部分。

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.这会将打印的 output 暂时重定向到 log.txt 文件。

You expect the print statements that are inside reporterror() to also write to the file.您希望reporterror()中的打印语句也写入文件。 However, this is not the case.然而,这种情况并非如此。 The only thing that happens here is that reporterror() returns 1 and prints each value in errors .这里唯一发生的事情是reporterror()返回1并打印errors中的每个值。 The print statements in reporterror() do not just inherit the file from the print statement in info() . reporterror()中的打印语句不仅仅从info()中的打印语句继承文件。 Below, I return a string that contains all of the values in errors followed by a line break.下面,我返回一个字符串,其中包含errors中的所有值,后跟一个换行符。 The print statement inside of info() will now print the output of reporterror() . info()中的 print 语句现在将打印reporterror()的 output。

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:如果要捕获所有output,不管打印语句是否指定output文件,可以通过终端将脚本output重定向到.txt文件:

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:值得注意的是,除非您有正当理由使用print()写入文件,否则最好只使用:

file.write(contents)

if you want all the print statements (AKA standard output stdout ) output to be redirect to a specific file just use this snippet如果您希望将所有打印语句(AKA 标准 output stdout )output 重定向到特定文件,只需使用此代码段

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

this shall do it.这将做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM