Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python函数在许多文件上打印到标准输出_Python - Fatal编程技术网

python函数在许多文件上打印到标准输出

python函数在许多文件上打印到标准输出,python,Python,最快的方法是什么。我需要换衣服 print('My text') 到 这里的要点是让我的应用程序从.NET检索控制台日志。 我需要很多脚本来检查每一个脚本,并从print更改为sys.stdout….print函数有内置的文件和刷新参数 print("My text", file=sys.stdout, flush=Flase) 你可以随意改变它 另一种方法是 import sys sys.stdout = open('file', 'w') print('My text

最快的方法是什么。我需要换衣服

print('My text')

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

print函数有内置的文件和刷新参数

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)

任何将打印功能设置为默认参数的方法??也就是说,只需说
print('My text')
?它将是:
def print\u和\u log(My\u text):print(My\u text,file=sys.stdout,flush=Flase)
。然后您可以使用该功能代替内置的
print
。否则,设置一个对象
config={'file':sys.stdout,'flush':False}
,然后使用
print('My text',**config)
print
已经默认为
sys.stdout
。使用functools:new\u print=functools.partial(print,file=sys.stdout)听起来您真的只是希望
-u
选项抑制标准输出上的缓冲。
import sys

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