-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstrip_whitespace.py
More file actions
28 lines (21 loc) · 872 Bytes
/
strip_whitespace.py
File metadata and controls
28 lines (21 loc) · 872 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
import sys
def strip_whitespace(file_path):
if file_path.endswith('.md'):
with open(file_path, 'r') as file:
lines = file.readlines()
# Check if the last line ends with a newline
has_newline_at_end = lines and lines[-1].endswith('\n')
with open(file_path, 'w') as file:
for i, line in enumerate(lines):
# Remove trailing spaces
line = line.rstrip()
# Add newline to all lines except the last one if it didn't originally have a newline
if i < len(lines) - 1 or has_newline_at_end:
line += '\n'
file.write(line)
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: python strip_whitespace.py <file_path>")
sys.exit(1)
file_path = sys.argv[1]
strip_whitespace(file_path)