wss71104307 发表于 2013-1-15 02:54:33

移除文件中的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 #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=0for 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    enumFiles(sys.argv)elif len(sys.argv)==3:    print "In the [%s]: scan the [%s] files" %(sys.argv,sys.argv)    enumFiles(sys.argv,sys.argv)else:   print "Error, the parameter is 2 at most!!!"if "__main__"==__name__:   main()
页: [1]
查看完整版本: 移除文件中的BOM头--in Python