python 读取配置文件总是报错 configparser.NoSectionError: No section:

news/2024/10/18 22:30:07/

本文为作者原创,禁止转载,违者必究法律责任!!!

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 configparserproDir = 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 文件

转载于:https://www.cnblogs.com/111testing/p/9609105.html


http://www.ppmy.cn/news/248250.html

相关文章

HEAP: HEAP: Free Heap block xx modified at xxafter it was freed 堆内存出现野指针错误

首先看下问题提示: HEAP[SimEng.exe]: HEAP: Free Heap block 000002CA51FC08A0 modified at 000002CA51FC0950 after it was freed Ok,碰到这种xxxx地址的报错提示就慌得一批。 解答 经过一种暴力式解谜,找到以下的原因(解谜…

6174问题

描述 假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b,然后用a-b替换原来这个数,并且继续操作。例如,从1234出发,依次可以得到4321-12343087、8730-3788352、8532-23586174,又…

openstack报错排查

openstack各个组件 openstack要保证各个组件能正常启动 否则创建实例等会报错 在启动openstack的时候 要查看各个服务是否正常启动了,如果有部分服务没有启动,则项目不能正常运行 [rootcontroller ~]# systemctl status open openstack-aodh-api.serv…

Spark执行spark-shell 报错 Cannot create directory /tmp/hive/root/xxx. Name node is in safe mode

1. 问题: Setting default log level to "WARN". To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel). 20/11/05 21:03:21 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your pla…

Cannot create directory /tmp/hive/root/...... Name node is in safe mode

因为hadoop的安全模式导致dbeaver不能连接hive报错 Cannot create directory /tmp/hive/root/… Name node is in safe mode 什么原因导致hadoop集群启动安全模式 Df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 32G 0 32G 0% /dev tmpfs 32G 0 32G 0% /dev/shm t…

11.6 MariaDB安装 11.7/11.8/11.9 Apache安装

2019独角兽企业重金招聘Python工程师标准>>> 11.6 MariaDB安装 准备工作 因为MariaDB的二进制包镜像源在国外地址,所以预先下载了该包到本地物理机,使用lrzsz工具将该包上传至虚拟机/usr/local/src目录进行安装。用的香港的云服务器下载 上传…

RTL系列WIFI模组Linux驱动移植

文章目录 1 驱动源码下载2 驱动移植2.1 源码的拷贝2.2 源代码的修改2.3 将wifi 驱动添加到内核中2.3.1 修改 Kconfig 文件2.3.2 修改Makefile2.3.3 配置Linux内核2.3.4 编译 RTL8723BU 驱动模块 3 编译出错解决3.1 _seqdump 错误3.2 __DATE__ 和 __TIME__ 错误3.3 strnicmp错误…

haproxy安装可能出现的问题

问题一: 启动提示错误: haproxy.service: Failed at step EXEC spawning /chris/haproxy/sbin/haproxy: Permission denied 解决方案: SELinux 搞的鬼。 关闭 sed -i s/enforcing/disabled/ /etc/selinux/config #永久 setenforce 0 #临…