#! /usr/bin/env python
import os


def _remove_trailing_whitespace(path):
    path = os.path.abspath(path)
    assert os.path.isdir(path)
    for root_directory, directory_names, file_names in os.walk(path):
        for file_name in file_names:
            if not file_name.endswith('.py'):
                continue
            file_path = os.path.join(root_directory, file_name)
            with open(file_path, 'r') as f:
                lines = f.read().splitlines()
            for i, line in enumerate(lines):
                lines[i] = line.rstrip()
            string = '\n'.join(lines)
            with open(file_path, 'w') as f:
                f.write(string)


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