Last active
May 31, 2017 05:47
-
-
Save mortymacs/61a7fc24f836426b78a06c3a56bd4169 to your computer and use it in GitHub Desktop.
Find all lines of import packages and files in a python file
This file contains hidden or 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
| pyfile = open("sample.py") | |
| ml = False # multiline import flag | |
| for line in pyfile: | |
| line = line.strip() | |
| if line.startswith("import"): | |
| print(line) | |
| elif line.startswith("from"): | |
| if line.find("(") >= 0: | |
| ml = True | |
| else: | |
| print(line) | |
| if ml is True: | |
| print(line, end=" ") | |
| if line.find(")") >= 0 and ml is True: | |
| print() | |
| if line.find(")") >= 0: | |
| ml = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment