Skip to content

Instantly share code, notes, and snippets.

@robertknight
Created October 3, 2019 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertknight/1ddb069150bef18eb0bc8520ac9f6764 to your computer and use it in GitHub Desktop.
Save robertknight/1ddb069150bef18eb0bc8520ac9f6764 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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