首页 / PYTHON / Python文件练习
Python文件练习
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python文件练习,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1642字,纯文字阅读大概需要3分钟。
内容图文
![Python文件练习](/upload/InfoBanner/zyjiaocheng/1219/59c94dce5c744658945df2e4ab8bded7.jpg)
练习内容:
使用Python管理ini文件:实现查询,添加,删除,保存操作。
练习目的:
1.掌握文件基本操作
2.认识ini文件
3.了解ConfigParser;
ini配置文件格式:
节:[session]
参数(键=值)
[port]
port1=3306
import ConfigParser
cfg=ConfigParser.ConfigParser()
vim test.txt
[userinfo]
name=nyan
pwd=password
[study]
python_base=15
python_junior=30
linux_base=15
cfg.read(‘test.txt‘)
cfg.sections()
cfg.items
for se in cfg.sections():
print se
print cfg.items(se)
cfg.set(‘userinfo‘,‘pwd‘,‘passw0rd‘):修改
cfg.set(‘userinfo‘,‘email‘,‘sibaoemail@qq.com‘):插入
cfg.remove_option(‘userinfo‘,‘email‘)
inimanage.py
import os
import os.path
import ConfugParser
...
1.dump ini
2.del section
3.del item
4.modify item
5.add section
6.save modify
...
class student_info(object):
def __init__(self.recordfile):
self.logfile = recordfile
self.cfg = ConfigParser.ConfigParser()
def cfg_load(self):
self.cfg.read(self.logfile)
def cfg_dump(self):
se_list = self.cfg.sections()
print "================="
for se in se_list:
print se
print self.cfg.items(se)
print "================="
def delete_item(self,section,key):
self.cfg.remove_option(setion,key)
def delete_section(self , section):
self.cfg.remove_section(section)
def add_section(self,section):
self.cfg.add_section(section)
def set_item(self,section,key,value):
self.cfg.set(section,key,value)
def save(self):
fp = oopen(‘test.txt‘,‘w‘)
self.cfg.write(fp)
dp.close()
if __name__ == ‘__main__‘:
info = student_info(‘test.txt‘)
info.cfg_load()
info.cfg_dump()
info.set_item(‘userinfo‘,‘pwd‘,‘passw0rd‘)
info.cfg_dump()
info.add_section(‘login‘)
info.set_item(‘login‘,‘2015-0511‘,‘20‘)
info.cfg_dump()
info.save()
原文:http://www.cnblogs.com/Nyan-Workflow-FC/p/5657253.html
内容总结
以上是互联网集市为您收集整理的Python文件练习全部内容,希望文章能够帮你解决Python文件练习所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。