Skip to content

Instantly share code, notes, and snippets.

@xtqqczze
Created September 3, 2025 10:24
Show Gist options
  • Save xtqqczze/91a99b7bb113b7ee9ba3442616bdddd8 to your computer and use it in GitHub Desktop.
Save xtqqczze/91a99b7bb113b7ee9ba3442616bdddd8 to your computer and use it in GitHub Desktop.
This file was generated by ChatGPT on 2025-09-03
import os
import pathlib
import sys
def ensure_final_newline(root_dir=".", extension=".cs"):
"""
Ensures that all files with the given extension under root_dir
end with exactly one newline.
"""
if not extension.startswith("."):
extension = "." + extension # normalize
root = pathlib.Path(root_dir)
files = list(root.rglob(f"*{extension}"))
for file in files:
with open(file, "rb") as f:
content = f.read()
# Normalize line endings to LF for check
text = content.decode("utf-8", errors="ignore")
# Strip all trailing newlines, then add exactly one
fixed = text.rstrip("\r\n") + "\n"
if text != fixed:
with open(file, "w", encoding="utf-8", newline="\n") as f:
f.write(fixed)
print(f"Fixed: {file}")
else:
print(f"OK: {file}")
if __name__ == "__main__":
# Usage: python script.py [root_dir] [extension]
root_dir = sys.argv[1] if len(sys.argv) > 1 else "."
extension = sys.argv[2] if len(sys.argv) > 2 else ".cs"
ensure_final_newline(root_dir, extension)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment