python3运行时候报错集锦:读取文件报错
1、关于读取文件报错:
命令执行到cf.read(cfpath),出现如下报错:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa0 in position 16: illegal multibyte sequence
1 # @Author:lsj
2 # @version V1.0
3 # -*- coding:UTF-8 -*-
4
5 import configparser
6 import os
7
8 def readConfig():
9 # 实例化一个cf对象
10 cf = configparser.ConfigParser()
11 # 获取上级目录(获取当前路径)
12 cfpath = os.path.dirname(os.path.abspath("."))+"\\config\\config.ini"
13 # 读取路径文件
14 cf.read(cfpath)
15 # 以上读取方式会出现报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa0 in position 16: illegal multibyte sequence 改进读取方式如下可以避免报错
16 # cf.read(cfpath,encoding='UTF-8')
17 # 获取参数
18 # cf.get("browser","bowserName")
19 print(cf.get("browser","browserName"))
20
21 if __name__ == "__main__":
22 readConfig()
修改成:cf.read(cfpath,encoding='UTF-8')