File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #! /usr/bin/env python
2+
3+ "Replace tabs with spaces in argument files. Print names of changed files."
4+
5+ import os
6+ import sys
7+ import string
8+ import getopt
9+
10+ def main ():
11+ tabsize = 8
12+ try :
13+ opts , args = getopt .getopt (sys .argv [1 :], "t:" )
14+ if not args :
15+ raise getopt .error , "At least one file argument required"
16+ except getopt .error , msg :
17+ print msg
18+ print "usage:" , sys .argv [0 ], "file ..."
19+ return
20+
21+ for file in args :
22+ process (file , tabsize )
23+
24+ def process (file , tabsize ):
25+ try :
26+ f = open (file )
27+ text = f .read ()
28+ f .close ()
29+ except IOError , msg :
30+ print "%s: I/O error: %s" % (`file` , str (msg ))
31+ return
32+ newtext = string .expandtabs (text , tabsize )
33+ if newtext == text :
34+ return
35+ backup = file + "~"
36+ try :
37+ os .unlink (backup )
38+ except os .error :
39+ pass
40+ try :
41+ os .rename (file , backup )
42+ except os .error :
43+ pass
44+ f = open (file , "w" )
45+ f .write (newtext )
46+ f .close ()
47+ print file
48+
49+ if __name__ == '__main__' :
50+ main ()
You can’t perform that action at this time.
0 commit comments