写好的python程序,在Linux运行环境中出错。进行测试后,发现显示错误如下:
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: BaseConfig.cfg, line: 22
'\xef\xbb\xbf#\xe5\x9f\xba\xe6\x9c\xac\xe9\x85\x8d\xe7\xbd\xae\xe4\xbf\xa1\xe6\x81\xaf\n'
查相关资料之后,显示:
在window下面用记事本编辑文件的时候,如果保存为UNICODE或UTF-8,分别会在文件的开头加上两个字节“\xFF\xFE”和三个字节“\xEF\xBB\xBF”。
修改方法如下:
方法1:删除该文件,在Linux上动手写配置文件;
方法2:运行如下程序:
import sys
import getopt
import re
def remove_BOM(config_path):
content = open(config_path).read()
content = re.sub(r"\xfe\xff","", content)
content = re.sub(r"\xff\xfe","", content)
content = re.sub(r"\xef\xbb\xbf","", content)
open(config_path, ‘w‘).write(content)
if __name__ == "__main__":
opts, args = getopt.getopt(sys.argv, "h", ["help"])
remove_BOM(args[1])
方法3:运行cat A.conf > A.conf 即可