Last active
June 19, 2018 11:37
-
-
Save devos50/31ee8ac2e1758ccccbd74d2a8c5985c5 to your computer and use it in GitHub Desktop.
Extract all print statements from a Python project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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