Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove __future__ imports from Python files
import sys
files = sys.argv[1:]
for path in files:
print(f"Updating {path}")
out_lines = []
prev_line_was_future_import = False
with open(path) as file:
for line in file:
is_future_import = line.startswith("from __future__")
if not is_future_import and (not prev_line_was_future_import or line.strip() != ""):
out_lines.append(line)
prev_line_was_future_import = is_future_import
file = open(path, "w")
file.write("".join(out_lines))
file.close()
@robertknight

This comment has been minimized.

Copy link
Owner Author

robertknight commented Oct 3, 2019

This script removes __future__ imports from Python files. It improves over running 2to3 by removing the lines rather than just replacing them with blank lines and it also removes any trailing new lines that separated them from the next comment or block of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.