-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_trailing_whitespaces
More file actions
executable file
·47 lines (38 loc) · 979 Bytes
/
remove_trailing_whitespaces
File metadata and controls
executable file
·47 lines (38 loc) · 979 Bytes
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
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
import argparse
import os
import sys
_TEXT_FILE_EXTENSIONS = {
'.cc',
'.css',
'.cpp',
'.html'
'.py',
'.js',
}
def IsValidTextFile(file_name):
_, ext = os.path.splitext(file_name)
return ext in _TEXT_FILE_EXTENSIONS
def main(args):
parser = argparse.ArgumentParser('Remove trailing white spaces')
parser.add_argument('path')
options = parser.parse_args(args)
path = options.path
if not os.path.exists(path):
print '%s does not exist' % path
return 1
for root, subdirs, files in os.walk(path):
for file_name in files:
if not IsValidTextFile(file_name):
continue
filepath = os.path.join(root, file_name)
lines = []
with open(filepath, 'r') as f:
for l in f:
lines.append(l.rstrip())
formatted_data = '\n'.join(lines)
with open(filepath, 'w') as f:
f.write(formatted_data)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))