相关疑难解决方法(0)

暂时重定向stdout/stderr

是否可以在Python中暂时重定向stdout/stderr(即在方法的持续时间内)?

编辑:

当前解决方案(我最初记得但后来忘记了)的问题在于它们没有重定向 ; 相反,他们只是完整地替换了流.因此,如果一个方法有一个本地副本,一个变量的任何原因(例如,由于流被作为参数传递的东西),它不会工作.

有解决方案吗

python redirect stdout stderr

55
推荐指数
5
解决办法
6万
查看次数

将cProfile结果保存到可读的外部文件中

我正在cProfile尝试尝试分析我的代码:

pr = cProfile.Profile()
pr.enable()
my_func()   # the code I want to profile
pr.disable()
pr.print_stats()
Run Code Online (Sandbox Code Playgroud)

但是,结果太长,无法在Spyder终端中完全显示(看不到运行时间最长的函数调用...)。我也尝试使用保存结果

 cProfile.run('my_func()','profile_results')
Run Code Online (Sandbox Code Playgroud)

但是输出文件的格式不是人类可读的格式(尝试使用带.txt后缀和不带后缀)。

所以我的问题是如何将分析结果保存到人类可读的外部文件中(例如以.txt正确显示所有单词的格式)?

python text profiling cprofile python-3.x

6
推荐指数
4
解决办法
3928
查看次数

标签 统计

python ×2

cprofile ×1

profiling ×1

python-3.x ×1

redirect ×1

stderr ×1

stdout ×1

text ×1