I am attempting to load a cProfile profile, do some sorting and finessing, then output the results to a file. Based on the documentation, I thought I could simply pass a file object and the print_stats function would redirect to that stream.
Here is the code I am attempting to use:
import sys,pstats
s = open('output.txt', 'w')
p = pstats.Stats('profile.dat', s)
and here is the resulting error:
TypeError: Cannot create or construct a <class pstats.Stats at 0xbaa870> object from '<open file 'output.txt', mode 'w' at 0xb2ef60>''
I should also add that when I do not pass an object to the stream parameter, the output appears as normal in the terminal.
Looking at the source code, you'd have to pass the file as a
streamkeyword argument (not clear to me why it was implented like that...), like:See below the inline comment, and the
if "stream" in kwdsline.