0

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!

asked Mar 14, 2014 at 8:01
0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.