How do I remove  from the beginning of a file?

前端 未结 23 1062
攒了一身酷
攒了一身酷 2020-11-22 06:21

I have a CSS file that looks fine when I open it using gedit, but when it\'s read by PHP (to merge all the CSS files into one), this CSS has the following characters prepend

23条回答
  •  终归单人心
    2020-11-22 06:45

    This works for me!

    def removeBOMs(fileName):
         BOMs = ['',#Bytes as CP1252 characters
        'þÿ',
        'ÿþ',
        '^@^@þÿ',
        'ÿþ^@^@',
        '+/v',
        '÷dL',
        'Ýsfs',
        'Ýsfs',
        '^Nþÿ',
        'ûî(',
        '„1•3']
         inputFile = open(fileName, 'r')
         contents = inputFile.read()
         for BOM in BOMs:
             if not BOM in contents:#no BOM in the file...
                 pass
             else:
                 newContents = contents.replace(BOM,'', 1)
                 newFile = open(fileName, 'w')
                 newFile.write(newContents)
                 return None
    

提交回复
热议问题