Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Last active May 31, 2017 05:47
Show Gist options
  • Select an option

  • Save mortymacs/61a7fc24f836426b78a06c3a56bd4169 to your computer and use it in GitHub Desktop.

Select an option

Save mortymacs/61a7fc24f836426b78a06c3a56bd4169 to your computer and use it in GitHub Desktop.
Find all lines of import packages and files in a python file
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