-
Notifications
You must be signed in to change notification settings - Fork 0
/
space-remover.py
executable file
·56 lines (43 loc) · 1.49 KB
/
space-remover.py
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
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# ============================================================================
# space-remover - Script to remove trailing spaces and tabs from files
# Copyright (C) 2018 by Ralf Kilian
# Distributed under the MIT License (https://opensource.org/licenses/MIT)
#
# GitHub: https://github.com/urbanware-org/space-remover
# GitLab: https://gitlab.com/urbanware-org/space-remover
# ============================================================================
__version__ = "1.1.0"
import os
import shutil
import sys
import tempfile
temp = "spacerem_" + str(os.getpid()) + ".tmp"
file_in = sys.argv[1]
file_out = os.path.join(tempfile.gettempdir(), temp)
if not os.path.exists(file_in):
print "error: The file '%s' does not exist" % file_in
sys.exit(1)
if os.path.isfile(file_in):
fh_input = open(file_in, "r")
fh_output = open(file_out, "w")
for line in fh_input:
# Remove tabs at the end of lines
while "\t\n" in line:
line = line.replace("t\n", "\n")
while "\t\r\n" in line:
line = line.replace("\t\r\n", "\r\n")
# Remove spaces at the end of lines
while " \n" in line:
line = line.replace(" \n", "\n")
while " \r\n" in line:
line = line.replace(" \r\n", "\r\n")
# Write output
fh_output.write(line)
fh_input.close()
fh_output.close()
shutil.move(file_out, file_in)
else:
print "error: The path '%s' is not a file" % file_in
sys.exit(1)
# EOF