#!/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()
移除文件中的BOM头--in Python
最新推荐文章于 2020-04-05 14:51:09 发布
本文介绍了一个Python脚本,用于扫描当前目录下的所有JSP文件,去除BOM头,并将Windows换行符转换为Unix风格的换行符。用户可以通过参数指定目录和文件类型进行操作。
1624

被折叠的 条评论
为什么被折叠?



