"""Remove trailing whitespace from files in current path and sub directories."""

import os, glob

def scanpath(path):
  for filepath in glob.glob(os.path.join(path, '*')):
    if os.path.isdir(filepath):
      scanpath(filepath)
    else:
      trimwhitespace(filepath)

def trimwhitespace(filepath):
  handle = open(filepath, 'rb')
  stripped = ''
  flag = False
  for line in handle.readlines():
    stripped_line = line.rstrip() + '\n'
    if line != stripped_line:
      flag = True
    stripped += stripped_line
  handle.close()
  if flag:
    print('FAIL: %s' % filepath)
    overwritefile(filepath, stripped)
  else:
    print('OK: %s' % filepath)

def overwritefile(filepath, contents):
  handle = open(filepath, 'wb')
  handle.write(contents)
  handle.close()

if __name__ == '__main__':
  scanpath('.')

@@ -295,16 +295,85 @@
 epath):%0A
+  if filepath.endswith('.pyc') or filepath.endswith('~'):%0A    return%0A
   handle
@@ -624,11 +624,10 @@
 t('F
-AIL
+IX
 : %25s

Tweak trimwhitespace to ignore *.pyc, *~.