Created
September 28, 2011 20:41
-
-
Save rduplain/1249199 to your computer and use it in GitHub Desktop.
Get a module's docstring in Python without executing it.
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 code | |
def get_module_docstring(filepath): | |
"Get module-level docstring of Python module at filepath, e.g. 'path/to/file.py'." | |
co = compile(open(filepath).read(), filepath, 'exec') | |
if co.co_consts and isinstance(co.co_consts[0], basestring): | |
docstring = co.co_consts[0] | |
else: | |
docstring = None | |
return docstring |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My approach: