Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Created October 5, 2011 06:15
Show Gist options
  • Select an option

  • Save codebrainz/1263776 to your computer and use it in GitHub Desktop.

Select an option

Save codebrainz/1263776 to your computer and use it in GitHub Desktop.
Cleanup trailing spaces
#!/usr/bin/env python
import sys
filenames = sys.argv[1:]
def backup_file (fn):
open ("%s~" % fn, "w").write (open (fn, "r").read ())
for fn in filenames:
#backup_file (fn)
contents = open (fn, "r").read ()
lines = contents.split ('\n')
with open (fn, "w") as fobj:
for line in lines:
line = line.rstrip ()
fobj.write ("%s\n" % line)
contents = open (fn, "r").read ()
contents.rstrip ()
while contents[-1] in " \t\r\n":
contents = contents[:-1]
open (fn, "w").write ("%s\n" % contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment