python ini文件内容的读取
(1)新建一个项目,再次新建一个文件 test_cfg.ini

(2)再次新建 get_test_cfg.py,用来读取/写入/更改 ini的文件内容
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:lucky,time:2019-06-10 import ConfigParser cfg1 = "test_cfg.ini" conf = ConfigParser.ConfigParser()
conf.read(cfg1) #读取ini文件中的内容
print conf.get("email","smtp_server")
print conf.get("Account information","username")
print conf.items("Account information") #获取到Account information中的所有内容,返回字典类型
print conf.options("Account information") #获取到Account information中的变量名 #向ini中添加内容
print conf.add_section("Account")
print conf.set("Account","title","")
print conf.write(open("test_cfg.ini","w+")) #向ini中修改内容
conf.set("Account","title","")
conf.write(open("test_cfg.ini","w+"))
如上是最简单的方式,另外一个方式是,我们可以将读取配置文件的信息单写一个py文件,再从需要调用的py文件中直接读取即可,详见如下:
(1)新建 read_test_cfg.py 文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:lucky,time:2019-06-10 import ConfigParser cfg1 = "test_cfg.ini" conf = ConfigParser.ConfigParser()
conf.read(cfg1) #读取ini文件中的内容
smtp_server = conf.get("email","smtp_server")
username = conf.get("Account information","username")
(2)新建 test.py 文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:lucky,time:2019-06-10 import read_test_cfg smtp = read_test_cfg.smtp_server
user = read_test_cfg.username print smtp
print user
打印结果:

python ini文件内容的读取的更多相关文章
- python yaml文件内容的读取
示例: (1)host_header.yaml 文件中的内容 host: https://testapp.goodiber.com/v2/ #dev1的测试环境域名 #请求接口的请求头中的共用参数 ...
- Python 输出文件内容到网络端口
Python 输出文件内容到网络端口 $ cat mySocketTest.py import sys import time import socket if __name__ == "_ ...
- python 修改文件内容
python 修改文件内容 一.修改原文件方式 1 def alter(file,old_str,new_str): 2 """ 3 替换文件中的字符串 4 :param ...
- [python IO学习篇] 补充.py文件是中文, .ini文件内容是中文
python 代码文件的编码.py文件默认是ASCII编码,中文在显示时会做一个ASCII到系统默认编码的转换,这时就会出错:SyntaxError: Non-ASCII character.需要在代 ...
- JAVA 创建TXT文件,写入文件内容,读取文件内容
[java] view plain copy package com.abin.facade.ws.mail.function; import java.io.BufferedReader; i ...
- python改动文件内容,不须要read,write多个动作。
python 要改动文件内容,经常使用 是先read.后write , 再 rename.非常不爽. 比方:须要 把 yuv_dir ="../HD/" # &q ...
- Python修改文件内容
工作中要写个脚本来修改文件的内容,然后就写了一个刷子: #coding:utf8 import os def modify_file(old_file, new_version, old_versio ...
- python 根据文件的编码格式读取文件
因为各种文件的不同格式,导致导致文件打开失败,这时,我们可以先判断文件的编码吗格式,然后再根据文件的编码格式进行读取文件 举例:有一个data.txt文件,我们不知道它的编码格式,现在我们需要读取文件 ...
- python 修改文件内容3种方法
原文链接:https://www.cnblogs.com/wc-chan/p/8085452.html def alter(file,old_str,new_str): ""&qu ...
随机推荐
- elasticsearch7.1.1【win】下载安装
下载:https://www.elastic.co/cn/downloads/elasticsearch 历史版本下载:https://www.elastic.co/cn/downloads/past ...
- python while循环 - python基础入门(9)
经过昨天的学习,相信大家已经对 python的条件判断表达式if/else 有一定的了解了,那么我们今天配合昨天的课程讲解一个新概念 – while循环 . 都说程序源于生活,假如有这样一个场景:老师 ...
- opencv轮廓外接矩形
1.寻找轮廓 api void cv::findContours( InputOutputArray image, OutputArrayOfArrays contours, OutputArray ...
- 基于tesseract-OCR进行中文识别
1. 环境准备 1.1 下载 下载Tesseract-OCR安装包,地址为: https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w32- ...
- sqlserver交换数据行中的指定列
<!-- 次序上移下移 --> <update id="upOrDown" parameterType="java.util.Map"> ...
- windows使用sqlpus连接oracle 数据库
1.先安装好Instant Client程序. 2.打开cmd 执行sqlplus命令,如果出现如下图 2.说明需要把oracle install 的bin目类 添加系统环境path中,如下图添加环境 ...
- NationalInstruments project
//using NationalInstruments.NI4882; //请将项目文件中的"AutoGenerateBindingRedirects"属性设置为true //ht ...
- java日志框架系列(6):logback框架encoder详解
1.Encoder 1.encoder功能 Encoder 负责两件事,一是把事件转换为字节数组,二是把字节数组写入输出流. 注意:在logback 0.9.19 版之前没有 encoder. 在之前 ...
- 【C++札记】类的分离式写法
介绍 类的分离式写法,使得代码更加规范,增强了阅读性. 分离式写法的规则: 1.类的变量:写在类的里面 2.成员函数:类中写函数的声明,函数的定义写在类体外. 3.写在类外函数定义时,类名前加限定(O ...
- EXTI中断开关点亮LED源码
在KEY点亮LED源码的基础上 USER下新建EXIT文件夹,新建bsp_exit.c和bsp_exit.h,添加到工程中(魔术棒添加头文件所在文件夹) bsp_exit.h内容 #ifndef BS ...