首页 > 解决方案 > python函数在许多文件上打印到stdOut

问题描述

最快的方法是什么。我需要改变

print('My text')

sys.stdout.write('My text')
 
sys.stdout.flush()

这里的重点是让我的 .NET 应用程序检索控制台日志。我有很多脚本要检查每一个并从 print 更改为 sys.stdout ....

标签: python

解决方案


打印功能具有内置文件和刷新参数

print("My text", file=sys.stdout, flush=Flase)

你可以随心所欲地改变它

另一种方法是

import sys
sys.stdout = open('file', 'w')
print('My text')
# all the other code #
sys.stdout.close()

也正如@Stefano 在评论中所说,您可以为此任务创建新功能

import sys

def my_print(text):
    print(text, file=file_path, flush=True)

推荐阅读