How can I parse the whole python script? as the following:
test.py:
import app
import _ast
import ast
if __name__ == "__main__":
## as1t = compile("app.py","<string>","exec",_ast.PyCF_ONLY_AST)
p = ast.parse("app.py")
print(ast.dump(p))
It parses the String "app.py" instead of the actual script. How to realize it? Thank you very much!
1 Answer 1
ast.parse() expects the code text, not the file name:
import ast
with open('app.py') as fp:
code = fp.read()
tree = ast.parse(code)
print ast.dump(tree)
answered Mar 14, 2014 at 8:06
bereal
34.7k8 gold badges65 silver badges111 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py