网站首页 > 技术文章 正文
一、读取配置文件
我的目录如下,在config下有一个config.ini配置文件
配置文件内容
# 定义config分组
[config]
platformName=Android
appPackage=com.romwe
appActivity=com.romwe.SplashActivity
# 定义cmd分组
[cmd]
viewPhone=adb devices
startServer=adb start-server
stopServer=adb kill-server
install=adb install aaa.apk
id=1
weight=12.1
isChoice=True
# 定义log分组
[log]
log_error=true
在test001.py中编写读取配置文件的脚本代码
import configparser
# 实例化configParser对象
config = configparser.ConfigParser()
# -read读取ini文件
config.read('C:\\Users\\songlihui\\PycharmProjects\\AutoTest_02\\config\\config.ini', encoding='GB18030')
# -sections得到所有的section,并以列表的形式返回
print('sections:' , ' ' , config.sections())
# -options(section)得到该section的所有option
print('options:' ,' ' , config.options('config'))
# -items(section)得到该section的所有键值对
print('items:' ,' ' ,config.items('cmd'))
# -get(section,option)得到section中option的值,返回为string类型
print('get:' ,' ' , config.get('cmd', 'startserver'))
# -getint(section,option)得到section中的option的值,返回为int类型
print('getint:' ,' ' ,config.getint('cmd', 'id'))
print('getfloat:' ,' ' , config.getfloat('cmd', 'weight'))
print('getboolean:' ,' ', config.getboolean('cmd', 'isChoice'))
"""
首先得到配置文件的所有分组,然后根据分组逐一展示所有
"""
for sections in config.sections():
for items in config.items(sections):
print(items)
运行test001.py文件,展示结果如下
二、写入配置文件
import configparser
# 实例化configParser对象
config = configparser.ConfigParser()
# -read读取ini文件
config.read('C:\\Users\\songlihui\\PycharmProjects\\AutoTest_02\\config\\config.ini', encoding='GB18030')
list = []
list = config.sections()# 获取到配置文件中所有分组名称
if 'type' not in list:# 如果分组type不存在则插入type分组
config.add_section('type')
config.set('type', 'stuno', '10211201')# 给type分组设置值
config.remove_option('type', 'stuno')# 删除type分组的stuno
config.remove_section('tpye')# 删除配置文件中type分组
o = open('C:\\Users\\songlihui\\PycharmProjects\\AutoTest_02\\config\\config.ini', 'w')
config.write(o)
o.close()#不要忘记关闭
写入和删除的操作全在代码中了,有详细的注释。不过写入的时候一定要添加判断,否则如果已经存在的话继续添加会报异常!!!!!!!!!
- 上一篇: 第41讲 基本文件操作(和猫妹学Python)
- 下一篇: 自动化文件管理的6个Python脚本模板
猜你喜欢
- 2025-03-28 Python os模块完全指南:轻松玩转文件管理与系统操作
- 2025-03-28 和尧名大叔一起从0开始学Python编程-简单读写文件
- 2025-03-28 【高效办公系列】一步一步教你使用Python下载文件
- 2025-03-28 如何用python打开文件(python怎样打开文件夹)
- 2025-03-28 Python内容写入excel文件(Excel写入)
- 2025-03-28 自动化文件管理的6个Python脚本模板
- 2025-03-28 第41讲 基本文件操作(和猫妹学Python)
- 2025-03-28 一日一技:python中的文件处理(01)
- 2025-03-28 [oeasy]python0010_怎么用命令行保存文件
- 2025-03-28 深度剖析Python配置文件:类型、应用及最佳实践
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)