python - python函数在许多文件上打印到stdOut
问题描述
最快的方法是什么。我需要改变
print('My text')
至
sys.stdout.write('My text')
sys.stdout.flush()
这里的重点是让我的 .NET 应用程序检索控制台日志。我有很多脚本要检查每一个并从 print 更改为 sys.stdout ....
解决方案
打印功能具有内置文件和刷新参数
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)
推荐阅读
- pyqt - 使用 PyQt5 将按钮连接到不同选项卡上的功能
- javascript - Puppeteer-page.evaluate() 没有按预期工作
- php - 使用数组并将图像链接输出到代码?
- cmake - 交叉编译:如何在使用 cmake 时设置链接器?
- ios - 未从 Azure 接收特定设备的通知
- docker - 在 docker 容器中登录以进行调试
- apple-push-notifications - 在终止模式下尝试 2-3 次后 iOS 14+ VoIP 推送块
- computer-vision - 在点云中计算和查看 3d 对象的方向的步骤是什么?
- c# - 如何以编程方式在 Winforms C# 中添加面板
- html - 如何使用 CSS 在侧边栏中制作目录?