Skip to content

Instantly share code, notes, and snippets.

@stevedoyle
Last active December 14, 2015 05:58
Show Gist options
  • Save stevedoyle/5038893 to your computer and use it in GitHub Desktop.
Save stevedoyle/5038893 to your computer and use it in GitHub Desktop.
Clean whitespace from source files. Remove trailing whitespace at the end of files and converts tabs to spaces.
#!/usr/bin/env python
#########################################################################
# Script to clean up whitespace in source files.
# - Removes trailing whitespace at the end of lines
# - Converts tabs to spaces (4 spaces = 1 tab)
#
# Copyright 2013 Stephen Doyle
#########################################################################
import sys
import shutil
def cleanup(f):
# make a backup
shutil.copyfile(f, f+".bak")
lines = file(f, 'r').readlines()
outf = file(f, 'w')
for line in lines:
line = line.rstrip().replace("\t", " ")
print >>outf, line
if __name__ == "__main__":
files = sys.argv[1:]
for f in files:
cleanup(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment