Skip to content

Instantly share code, notes, and snippets.

@techtonik
Last active Nov 9, 2016
Embed
What would you like to do?
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