Skip to content

Instantly share code, notes, and snippets.

@wil wil/
Created Oct 24, 2011

What would you like to do?
Look for the for/else construct in python code
#!/usr/bin/env python
# Parses a .py file and look for for-else constructs
# Usage:
import sys
import ast
def check_file(filename):
with open(filename, "r") as f:
module = ast.parse(, filename, 'exec')
for node in ast.walk(module):
if isinstance(node, ast.For) and node.orelse:
print "found for-else statement in %s:%d" % (filename, node.lineno)
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.