Find print statements in Python code
12th of April 2005
Do you, like me, use the powerful print function to debug your Python code? Do you, like me, sometimes forget to remove lines with print and ship this out to less development-like environments? Do you, like I did, need a way to check for forgotten print statements?
I've written a dumb-simple script that scans through a directory for print statements. It uses a recursive grep command so this won't work for you hopeless Windows people :)
Download it and put it in your ~/bin (you might want to read it through) then all you have to do is to run it against a directory where there are lots of .py files:
$ find_print_statements.py ~/dev/mypythonmodule
If it doesn't work perhaps you can patch it and let me know. This might not be for the faint-hearted. There might be serious security concerns unless you have full control over your users and su account. I take no responsibility if this script rm -fr / your hd.
Here's some example output:
$ find_print_statements.py CheckoutableTemplates
---- CheckoutableTemplates/CTFiles.py:51 --------
if DEBUG:
print "CT|",
if type(debug_output)==StringType:
print debug_output
else:
---- CheckoutableTemplates/CTFiles.py:53 --------
if type(debug_output)==StringType:
print debug_output
else:
pprint(debug_output)
open('output.log','a').write(debug_output.strip()+'\n')