-
Notifications
You must be signed in to change notification settings - Fork 0
/
removebom.py
52 lines (40 loc) · 979 Bytes
/
removebom.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
# -*- encoding: utf-8 -*-
# personally I suggest you use git-hook to run this program before commit
"remove bom for all utf-8 encoded file."
import codecs
import os
def processFile(path: str):
"remove bom for a single file"
rb = open(path, "rb")
if rb.read(3) == codecs.BOM_UTF8:
rb.close()
rt = open(path, 'r+', encoding='utf-8')
s = rt.read()
rt.seek(0)
rt.truncate(0)
rt.write(s[1:])
rt.flush()
rt.close()
print("process %s" % path)
print("git add %s" % path)
os.system("git add %s" % path)
else:
rb.close()
print("skip %s" % path)
EXTS = [
'cs',
'csproj',
'sln',
'resx',
'yml',
'md',
'py'
]
def main():
"main function"
for p, j, files in os.walk("."):
for f in files:
if f.split('.')[-1] in EXTS:
processFile(os.path.join(p, f))
if __name__ == "__main__":
main()