python 读取配置文件总是报错 configparser.NoSectionError: No section:
本文为作者原创,禁止转载,违者必究法律责任!!!
python 读取配置文件总是报错 configparser.NoSectionError: No section:
无论是 python2的版本,还是python3的版本在读取配置文件时都会出现上面的报错
解决方案:
1,配置文件尽量设置为config.txt类型的文本文件,不要设置为onfig.ini
[baseUrl]
url_baidu = https://www.baidu.com
url_taobao = https://www.taobao.com
[email]
email_baidu = baidu@qq.com
email_taobao = taobao@qq.com
2,写一个 readConfig.py 的文件用来读取 config.txt 配置文件,具体代码如下
import os
import configparser proDir = os.path.split(os.path.realpath(__file__))[0]
# proDir = os.path.dirname(os.path.realpath(__file__)) 与上面一行代码作用一样
configPath = os.path.join(proDir, "configs.txt")
path = os.path.abspath(configPath)
print(configPath)
print(path) conf = configparser.ConfigParser()
# 下面3种路径方式都可以
conf.read(path)
# conf.read(configPath)
# conf.read("D:/python2.7/practises/practise/configs.txt") url_baidu = conf.get('baseUrl', 'url_baidu')
url_taobao = conf.get('baseUrl', 'url_taobao')
email_baidu = conf.get('email', 'email_baidu')
email_taobao = conf.get('email', 'email_taobao') print url_baidu
print url_taobao
print email_baidu
print email_taobao
输出的内容是:
D:\python2.7\venv\Scripts\python.exe D:/python2.7/practises/practise/readConfig.py
D:\python2.7\practises\practise\configs.txt
D:\python2.7\practises\practise\configs.txt
https://www.baidu.com
https://www.taobao.com
baidu@qq.com
taobao@qq.com
3,但是我将 config.txt 类型的文本文件 设置为 onfig.ini 时,就会出现 configparser.NoSectionError: No section: ,很有可能是因为没有读取到 onfig.ini 文件
python 读取配置文件总是报错 configparser.NoSectionError: No section:的更多相关文章
- python执行报错 configparser.NoSectionError: No section: 'section_1'
场景:请求获取验证码模块regVC.py读取配置文件config.ini时,regVC.py模块单独执行正常,但通过run_all.py模块批量执行时报错,找不到section 解决办法:配置文件路径 ...
- 记一次用python 的ConfigParser读取配置文件编码报错
记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...
- 【spring boot】使用注解@ConfigurationProperties读取配置文件时候 报错 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rocketmqAutoConfiguration': Unsatisfied dependenc
如题,配置文件如下: #注册中心配置 eureka: instance: instanceId: ${spring.application.name}:${random.int} hostname: ...
- python读取数据库mysql报错
昨天在学习PYTHON读取数据库的知识时,一直在报错,找不到原因. 最后同事说是语法错误. import sysreload(sys)sys.setdefaultencoding('gb18030') ...
- python读取xml文件报错ValueError: multi-byte encodings are not supported
1.在使用python对xml文件进行读取时,提示ValueError: multi-byte encodings are not supported 很多贴子上说把xml的编码格式改为,就可以正常执 ...
- python读取配置文件 ConfigParser
Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...
- python读取配置文件&&简单封装
之前有做过把爬虫数据写到数据库中的练习,这次想把数据库信息抽离到一个ini配置文件中,这样做的好处在于可以在配置文件中添加多个数据库,方便切换(另外配置文件也可以添加诸如邮箱.url等信息) 1.co ...
- python读取配置文件的方式
python读取配置文件的方式 1.从config.ini中读取,后缀无所谓,文件名字也无所谓,不过config.ini是常用写法,所谓见名知意 config.ini内容: [global] ip = ...
- ubuntu上跑python连接pg,报错 ImportError: No module named psycopg2
ubuntu上跑python连接pg,报错 ImportError: No module named psycopg2 root@pgproxy1:~# python /home/zxw/PGWri ...
随机推荐
- jenkins 集成redmine
安装 可以使用jenkins的插件管理页面进行安装,也可以使用其id(redmine)在镜像中进行安装并重启镜像即可. 插件安装确认 重新启动后确认此插件已经安装完毕 设定内容 系统管理 -> ...
- Docker监控:google/cadvisor
Docker自带了容器监控功能,可以对容器进行相关的性能监控,指标查看 主要包括: 主机的CPU情况和使用量 主机的内存情况和使用量 主机的本地镜像情况 主机的容器运行情况 常规使用docker ps ...
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
[BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...
- Spring Boot 商城项目
Spring Boot 商城项目 angularJS Demo1 <html> <head> <title>angularJS Demo1</title> ...
- CentOS 6.8下更改Apache默认网站安装目录
首先,当我们搭建好LAMP环境后,Apache服务器默认的网站安装目录是/var/www/html 然而我们搭建项目时为了方便,我们是按照自己的意愿更改网站目录的 现在假设,按照意愿设定的网站目录为/ ...
- Nodejs express、html5实现拖拽上传
一.前言 文件上传是一个比较常见的功能,传统的选择方式的上传比较麻烦,需要先点击上传按钮,然后再找到文件的路径,然后上传.给用户体验带来很大问题.html5开始支持拖拽上传的需要的api.nodejs ...
- ajax跨域获取返回值
js代码 $.ajax({ async:false, url: 'https://***/api/prepareApi.getDanMu?sqlMapId=findBarrage', // 跨域URL ...
- iptables、防火墙配置、NAT端口映射
一,配置一个filter表放火墙 (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L -n Chain INPUT (policy ACCEPT) targ ...
- php无限极分类递归与普通
1. 递归 public function getInfo(){$data=$this->select();$arr=$this->noLimit($data,$f_id=0,$level ...
- ubuntu 添加用户到已存在的组
sudo adduser 用户名 组名 sudo minicom –s 配置 minicom访问ttyUSB0没权限,发现属于dialout 组 james@james-OptiPlex-380: ...