-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtrimwstrail
executable file
·38 lines (33 loc) · 1.31 KB
/
trimwstrail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
import os
import sys
def trim_py_files(directories):
"""Remove trailing whitespace on all .py files in the given directories.
"""
nchanged = 0
print(directories)
for directory in directories:
for root, dirs, files in os.walk(directory):
if ".git" in root:
continue
for fname in files:
filename = os.path.join(root, fname)
if fname.endswith('.py'):
with open(filename, 'rb') as f:
code1 = f.read().decode()
lines = [line.rstrip() for line in code1.splitlines()]
while lines and not lines[-1]:
lines.pop(-1)
lines.append('') # always end with a newline
code2 = '\n'.join(lines)
if code1 != code2:
nchanged += 1
print(' Removing trailing whitespace on', filename)
with open(filename, 'wb') as f:
f.write(code2.encode())
print('Removed trailing whitespace on {} files.'.format(nchanged))
if __name__ == '__main__':
if len(sys.argv) < 2:
trim_py_files(list(os.path.dirname(__file__)))
else:
trim_py_files(sys.argv[1:])