移除文件中的BOM头--in Python

#!/usr/bin/python
#Remove BOM head and replace the Windows linefeeds with the Unix linebreak.
#The script search the jsp files in the current directory by defalut.
#You can specify the directory and the file type in the parameters
#python rmbom.py [directory] [file type]
#For example, you can usr python rmbom.py /home .txt to reformat the all txt files in the home directory.
#
#warning: you should have the premission to read and write the files that you want to reform. 

import os, codecs, sys

def enumFiles(strDir=".", suffix=".jsp"):
  print "Scanning the [%s] file in [%s]" %(suffix, strDir)
  i=0
  for item in os.listdir(strDir):
     currltern=os.path.abspath(os.path.join(strDir, item))
     if os.path.isdir(currltern):
       enumFiles(currltern)
     else:
       if currltern.endswith(suffix):
         print "==>Now scanning: %s"%currltern,
         reencodeFile(currltern)
         i=i+1
         print "<==over"
  print "The number of the scanned files is [%d]" %i 

def reencodeFile(strFilePath):
  file=open(strFilePath, "rb")
  content=file.read().replace("\r\n", "\n").replace(codecs.BOM_UTF8,"")
  file.close()
  file=open(strFilePath, "wb")
  file.write(content)
  file.close()

def main():
  if len(sys.argv)==1:
    print "In the current directory: scan the [.jsp] files"
    enumFiles()
  elif len(sys.argv)==2:
    print "In the %s : scan the [.jsp] files" %sys.argv[1]
    enumFiles(sys.argv[1])
  elif len(sys.argv)==3:
    print "In the [%s]: scan the [%s] files" %(sys.argv[1],sys.argv[2])
    enumFiles(sys.argv[1],sys.argv[2])
  else: 
    print "Error, the parameter is 2 at most!!!"

if "__main__"==__name__:
   main()

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值