Skip to content

Instantly share code, notes, and snippets.

@devos50
Last active June 19, 2018 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devos50/31ee8ac2e1758ccccbd74d2a8c5985c5 to your computer and use it in GitHub Desktop.
Save devos50/31ee8ac2e1758ccccbd74d2a8c5985c5 to your computer and use it in GitHub Desktop.
Extract all print statements from a Python project
import ast
import os
import sys
location = os.path.abspath(sys.argv[1])
exclude_from_check = ['twisted/plugins/tunnel_helper_plugin.py']
for (dirpath, dirnames, filenames) in os.walk(location):
for filename in filenames:
if not filename.endswith('.py'):
continue
file_contents = ''
file_path = os.path.join(dirpath, filename)
if any([file_path.endswith(excluded_path) for excluded_path in exclude_from_check]):
continue
with open(file_path, 'r') as file:
file_contents = file.read()
if file_contents:
node = ast.parse(file_contents)
for subnode in ast.walk(node):
if isinstance(subnode, ast.Print):
print 'print at line {}, col {} in file {}'.format(subnode.lineno, subnode.col_offset, file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment