Python代码: Simple New Line Converter - CRLN DOS<=>UNIX,换行符替换工
01 #! /usr/bin/env python
02 # -*- coding: utf-8 -*-
03 #
04 # Simple New Line Converter for CR-LN of DOS & UNIX format
05 # default convert known text files under current folder
06 #
07 # by 半瓶墨水 ( realfun at gmail dot com )
08 # http://2maomao.com/blog/
09 import os
10 import sys
11
12 newline = "\r\n"
13 if len(sys.argv) > 1 and sys.argv[1] == 'unix':
14 newline = "\n"
15
16 #only convert files with the following extention
17 #NOTICE: spaces on HEAD/END are there on purpose!
18 exts = ' py html css js json txt php ini cpp h sql ini htm rb cmd bat '
19 fs = []
20 #for root, dirs, files in os.walk('d:/projects/py/django/fayaa'):
21 for root, dirs, files in os.walk('.'):
22 for f in files:
23 f = os.path.join(root, f)
24 #print "==>" + f
25 pos = f.rfind('.')
26 if pos == -1:
27 continue
28 if exts.find(' ' + f[pos+1:] + ' ') != -1:
29 fs.append(f)
30
31 for f in fs:
32 o = open(f, "r")
33 ls = o.readlines()
34 o.close
35 if len(ls):
36 print ">>", f
37 o = open(f, "wb")
38 #NOTICE: here I did what I want, remove ".rstrip()" if you don't like it!
39 ls = [l.rstrip() + newline for l in ls]
40 for l in ls:
41 o.write(l)
42 o.close()