六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 44|回复: 0

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

[复制链接]

升级  0.33%

57

主题

57

主题

57

主题

举人

Rank: 3Rank: 3

积分
201
 楼主| 发表于 2013-1-15 02:54:33 | 显示全部楼层 |阅读模式
#!/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, sysdef 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()
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表