资源简介


资源截图

代码片段和文件信息

import os sys codecs fnmatch stat

BUFSIZE = 4096
BOMLEN = len(codecs.BOM_UTF8)

NO_BOM_MESSAGE =        ‘  No BOM:         {}‘
REMOVING_BOM_MESSAGE =  ‘  Removing BOM:   {}‘
READ_ONLY_BOM_MESSAGE = ‘! ReadOnly BOM:   {}‘
NON_EXISTANCE_MESSAGE = ‘! Does not exist: {}‘

def file_has_bom(path):
    with open(path “rb“) as fp:
        chunk = fp.read(len(codecs.BOM_UTF8))
        return chunk.startswith(codecs.BOM_UTF8)

def remove_bom_from_file(path):
    with open(path “r+b“) as fp:
        chunk = fp.read(BUFSIZE)
        assert(chunk.startswith(codecs.BOM_UTF8))
        i = 0
        chunk = chunk[BOMLEN:]
        while chunk:
            fp.seek(i)
            fp.write(chunk)
            i += len(chunk)
            fp.seek(BOMLEN os.SEEK_CUR)
            chunk = fp.read(BUFSIZE)
        fp.seek(-BOMLEN os.SEEK_CUR)
        fp.truncate()

def remove_bom_from_directory(path pattern):
    for child_name in os.listdir(path):
        child_path = os.path.join(path child_name)
        if os.path.isdir(child_path):
            remove_bom_from_directory(child_path pattern)
        elif 1>0:
            if file_has_bom(child_path):
                if os.access(child_path os.W_OK):
                    print (REMOVING_BOM_MESSAGE.format(child_path))
                    remove_bom_from_file(child_path)
                else:
                    print (READ_ONLY_BOM_MESSAGE.format(child_path))
            else:
                # print NO_BOM_MESSAGE.format(child_path)
                pass

if len(sys.argv) < 2:
    print (‘Usage: remove_bom  [
...]‘)
    print (‘‘)
    print (‘Will remove UTF-8 BOM from files whith names that match ‘)
    print (‘found in the specified directories recusivly. If no directories‘)
    print (‘are specified the current working direcotry is used.‘)
    exit(1)

pattern = sys.argv[1]
directories = sys.argv[2:] or [ os.getcwd() ]
for directory in directories:
    if os.path.isdir(directory):
        remove_bom_from_directory(directory pattern)
    else:
        NON_EXISTANCE_MESSAGE.format(directory)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2021-01-06 02:50  QQchat\
     文件         176  2021-01-06 02:50  __MACOSX\._QQchat
     文件        2666  2019-08-11 10:56  QQchat\myqq.sql
     文件         232  2019-08-11 10:56  __MACOSX\QQchat\._myqq.sql
     文件        6148  2021-01-06 03:53  QQchat\.DS_Store
     文件         120  2021-01-06 03:53  __MACOSX\QQchat\._.DS_Store
     文件        1069  2017-11-28 08:20  QQchat\LICENSE
     文件         176  2017-11-28 08:20  __MACOSX\QQchat\._LICENSE
     目录           0  2021-01-06 03:33  QQchat\bin\
     文件         176  2021-01-06 03:33  __MACOSX\QQchat\._bin
     文件         552  2019-05-29 02:29  QQchat\.classpath
     文件         176  2019-05-29 02:29  __MACOSX\QQchat\._.classpath
     文件        2808  2021-01-06 03:32  QQchat\README.md
     文件         176  2021-01-06 03:32  __MACOSX\QQchat\._README.md
     文件          11  2017-11-28 08:20  QQchat\.gitignore
     文件         176  2017-11-28 08:20  __MACOSX\QQchat\._.gitignore
     目录           0  2018-11-28 11:17  QQchat\.settings\
     文件         176  2018-11-28 11:17  __MACOSX\QQchat\._.settings
     文件         363  2017-11-28 08:20  QQchat\.project
     文件         176  2017-11-28 08:20  __MACOSX\QQchat\._.project
     目录           0  2018-11-28 11:18  QQchat\lib\
     文件         176  2018-11-28 11:18  __MACOSX\QQchat\._lib
     文件        1995  2021-01-06 02:50  QQchat\myqq.iml
     目录           0  2021-01-06 03:53  QQchat\.idea\
     目录           0  2021-01-06 03:03  QQchat\src\
     文件         176  2021-01-06 03:03  __MACOSX\QQchat\._src
     目录           0  2021-01-06 03:33  QQchat\bin\server\
     文件        1459  2021-01-06 02:51  QQchat\bin\log4j.xml
     文件        6718  2021-01-06 02:51  QQchat\bin\log4j-1.2.dtd
     目录           0  2021-01-06 03:33  QQchat\bin\client\
     目录           0  2021-01-06 03:33  QQchat\bin\com\
............此处省略622个文件信息

评论

共有 条评论