- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathuntabify.py
executable file
·52 lines (46 loc) · 1.19 KB
/
untabify.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
#! /usr/bin/env python
"Replace tabs with spaces in argument files. Print names of changed files."
importos
importsys
importgetopt
defmain():
tabsize=8
try:
opts, args=getopt.getopt(sys.argv[1:], "t:")
ifnotargs:
raisegetopt.error, "At least one file argument required"
exceptgetopt.error, msg:
printmsg
print"usage:", sys.argv[0], "[-t tabwidth] file ..."
return
foroptname, optvalueinopts:
ifoptname=='-t':
tabsize=int(optvalue)
forfilenameinargs:
process(filename, tabsize)
defprocess(filename, tabsize, verbose=True):
try:
f=open(filename)
text=f.read()
f.close()
exceptIOError, msg:
print"%r: I/O error: %s"% (filename, msg)
return
newtext=text.expandtabs(tabsize)
ifnewtext==text:
return
backup=filename+"~"
try:
os.unlink(backup)
exceptos.error:
pass
try:
os.rename(filename, backup)
exceptos.error:
pass
withopen(filename, "w") asf:
f.write(newtext)
ifverbose:
printfilename
if__name__=='__main__':
main()