12

I am new to python programming.I have a python script and I am trying to profile it using cProfile command. I typed the following

python -m cProfile -o readings.txt my_script.py

It generated readings.txt. But when I try to open the file using any standard text editor or notepad, the file doesn't open properly. It doesn't contain the data

Can anyone please tell me how to store these statistics into an external file that can be opened using notepad??

I am using windows platform

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
user3217603
  • 123
  • 1
  • 4
  • I'm pretty sure the outcome of a profiler is not text editable. It's in the Pstat form. Look really quick at the documentation and it says how to open these files. – ZekeDroid Jan 31 '14 at 00:14

1 Answers1

18

The output file generated by the cProfile -o module isn't plaintext; it's a serialized pstats.Stats object. Rather than using the -o option, I generally just redirect stdout into the file I want to create.

python -m cProfile -s time my_script.py > profile.text 2>&1

Otherwise, you just need to use the pstats module to read the file and inspect its contents (see the documentation linked above).

Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
  • 1
    @user3217603 Yep, the Python all works exactly the same. If you don't specify the `-o` flag, the `cProfile` module prints the results to stdout. Just redirect stdout wherever you want the output to show up. I believe the syntax is identical to what I wrote above, but it's been a while since I did anything on *nix. – Henry Keiter Jan 31 '14 at 05:56