Skip to content

Instantly share code, notes, and snippets.

@techtonik
Last active November 9, 2016 10:41
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 techtonik/c86f0ea6a86ed3f38893 to your computer and use it in GitHub Desktop.
Save techtonik/c86f0ea6a86ed3f38893 to your computer and use it in GitHub Desktop.
Strip trailing whitespace from file
#!/usr/bin/env python2
"""\
strip trailing whitespace from file
usage: stripspace.py <file>
code is placed into public domain
"""
import sys
if len(sys.argv[1:]) != 1:
sys.exit(__doc__)
content = ''
outsize = 0
inp = outp = sys.argv[1]
with open(inp, 'rb') as infile:
content = infile.read()
with open(outp, 'wb') as output:
for line in content.splitlines():
newline = line.rstrip(" \t")
outsize += len(newline) + 1
output.write(newline + '\n')
print("Done. Stripped %s bytes." % (len(content)-outsize))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment