Skip to content

Instantly share code, notes, and snippets.

@mirekfranc
Created September 14, 2016 12:39
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 mirekfranc/fde56397943061ba3b2d424c87e8b754 to your computer and use it in GitHub Desktop.
Save mirekfranc/fde56397943061ba3b2d424c87e8b754 to your computer and use it in GitHub Desktop.
remove trailing newline at the end of the file / windows tools generate this crap a one needs to be compatible with them
import sys
if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
if len(sys.argv) < 2:
sys.exit(1)
with open(sys.argv[1], 'rb') as f:
oldline = None
for line in f:
if oldline:
sys.stdout.write(oldline)
oldline = line
if oldline[-2:] == '\r\n':
oldline = oldline[:-2]
if oldline[-1:] == '\n':
oldline = oldline[:-1]
sys.stdout.write(oldline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment