- 論壇徽章:
- 0
|
我寫的有個項目不知道怎么的再上傳后文件全加了bom頭,一個一個修改起來比較麻煩,所以查了下資料寫了個批處理的腳本,共享給大家看看。這個腳本如果在有中文目錄的環(huán)境下執(zhí)行可能會有點編碼問題,不過一般的程序文件好像沒中文目錄,哈哈- #coding=utf-8
- '''
- * 去除指定類型文件的bom頭
- * 版權(quán)所有
- * @author t6760915<t6760915@gmail.com>
- * @version $Id: trim_bom.py $
- '''
- import os
- import sys
- import codecs
- class TrimBom:
-
- basePath = ''
- fileList = []
- trimExtList = ['php', 'css', 'js', 'py', 'pl', 'html', 'htm']
- #去除字符串中的bom部分
- def trim_bom(self, str):
- if not str:
- return ''
-
- if len(str) < 3:
- return ''
-
- if str[:3] == codecs.BOM_UTF8:
- return str[3:]
-
- return str
- #去除某文件的bom頭
- def trim_file_bom(self, fileName):
- if not fileName:
- return False
-
- if not os.path.exists(fileName):
- return False
- try:
- fp = open(fileName, 'r')
- html_sourse = fp.read()
- fp.close()
- except:
- return False
- html_result = self.trim_bom(html_sourse)
- if html_result == html_sourse:
- return False
- try:
- fp = open(fileName, 'w')
- fp.write(html_result)
- fp.close()
- except:
- return False
- return True
- #獲取指定類型文件名列表
- def getFileListByExt(self, path):
- if not path:
- return False
- path = os.path.normpath(path)
- if not os.path.exists(path):
- return False
- if os.path.isfile(path) and path.split('.')[-1] in self.trimExtList:
- trimFlag = self.trim_file_bom(path)
- if trimFlag:
- print 'process %s success...' % path.replace(self.basePath, '').replace('\\', '/')
-
- elif os.path.isdir(path):
- fileNameList = os.listdir(path)
- for fileName in fileNameList:
- fileName = os.path.normpath('%s/%s' % (path,fileName))
- self.getFileListByExt(fileName)
-
- return False
- #運行函數(shù)入口
- def run(self, path):
- self.basePath = os.path.normpath(path)
- self.getFileListByExt(path)
- if __name__ == '__main__':
- if len(sys.argv) < 2:
- print 'USEAGE:python %s dirName' % __file__
- sys.exit(0)
-
- tObj = TrimBom()
- tObj.run(sys.argv[1])
復(fù)制代碼
trim_bom.rar
(971 Bytes, 下載次數(shù): 126)
2010-05-28 11:38 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|