SoFunction
Updated on 2024-12-19

python read and write ini file example (python read and write files)

Very similar to a java properties file.

xml file

Copy Code The code is as follows.

db_config.ini
[baseconf]
host=127.0.0.1
port=3306
user=root
password=root
db_name=evaluting_sys
[concurrent]
processor=20

The corresponding python code

Copy Code The code is as follows.

#!/usr/bin/python
# -*- coding:utf-8 -*-
#author:
#desc: use to db ops
#---------------------
#2012-02-18 created

#---------------------
import sys,os
import ConfigParser

class Db_Connector:
  def __init__(self, config_file_path):
    cf = ()
    (config_file_path)

    s = ()
    print 'section:', s

    o = ("baseconf")
    print 'options:', o

    v = ("baseconf")
    print 'db:', v

    db_host = ("baseconf", "host")
    db_port = ("baseconf", "port")
    db_user = ("baseconf", "user")
    db_pwd = ("baseconf", "password")

    print db_host, db_port, db_user, db_pwd

    ("baseconf", "db_pass", "123456")
    (open("config_file_path", "w"))
if __name__ == "__main__":
  f = Db_Connector("../conf/db_config.ini")

Getting results:

Copy Code The code is as follows.

section: ['concurrent', 'baseconf']
options: ['host', 'db_name', 'user', 'password', 'port']
db: [('host', '127.0.0.1'), ('db_name', 'evaluting_sys'), ('user', 'root'), ('password', 'root'), ('port', '3306')]
127.0.0.1 3306 root root

Generic module: support command line + import two forms
ini_op.py

Copy Code The code is as follows.

#!/usr/bin/python
# -*- coding:utf-8 -*-
#author:
#desc: use to read ini
#---------------------
#2012-02-18 created
#2012-09-02 changed for class support

#---------------------
import sys,os,time
import ConfigParser


class Config:
    def __init__(self, path):
        = path
        = ()
        ()
    def get(self, field, key):
        result = ""
        try:
            result = (field, key)
        except:
            result = ""
        return result
    def set(self, filed, key, value):
        try:
            (field, key, value)
            (open(,'w'))
        except:
            return False
        return True
def read_config(config_file_path, field, key):
    cf = ()
    try:
        (config_file_path)
        result = (field, key)
    except:
        (1)
    return result

def write_config(config_file_path, field, key, value):
    cf = ()
    try:
        (config_file_path)
        (field, key, value)
        (open(config_file_path,'w'))
    except:
        (1)
    return True

if __name__ == "__main__":
   if len() < 4:
      (1)

   config_file_path = [1]
   field = [2]
   key = [3]
   if len() == 4:
      print read_config(config_file_path, field, key)
   else:
      value = [4]
      write_config(config_file_path, field, key, value)

Second example


Copy Code The code is as follows.

import os
import ConfigParser

def main():
    cp = ()   
    cf = open(u"")
    (cf)

    secs = ()
    print ()

    for sec in secs:
        opts = (sec)
        for opt in opts:
            val = (sec, opt)
            val += "test....."
            (sec, opt, val)

    (open("", "w"))

if __name__ == '__main__':
    main()