Created
October 3, 2019 09:37
-
-
Save robertknight/1ddb069150bef18eb0bc8520ac9f6764 to your computer and use it in GitHub Desktop.
Remove __future__ imports from Python files
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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script removes
__future__
imports from Python files. It improves over running2to3
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.