python Read and Write Profile Operation Sample

  • 2021-07-09 08:32:03
  • OfStack

This article illustrates how python reads and writes configuration files. Share it for your reference, as follows:

When writing programs in compiled languages, configuration files are often used. As an agreed rule, ini files are generally used as configuration files. Of course, they are not absolute, but also XML and other files.

Configuration files are configuration parameters that are required when the program starts or runs. As a compiled language, they are almost always used, but python is a dynamic language. One of the major features of dynamic languages is parsing and executing. Therefore, in many cases, the parameters that need to be configured are usually written directly in the script. A common practice is to use a single file as the configuration file, such as django, which we often contact, and he will use settings. py and urls. py to configure some parameters. When you need to modify it, you can modify this py file directly.

Even so, python still provides a way to read the configuration file. When combined with other systems, it is usually used. To view the document, I realized a more general method of reading and writing configuration files


# -*- coding:utf-8 -*-
import ConfigParser
import os
class ReadWriteConfFile:
  currentDir=os.path.dirname(__file__)
  filepath=currentDir+os.path.sep+"inetMsgConfigure.ini"
  @staticmethod
  def getConfigParser():
    cf=ConfigParser.ConfigParser()
    cf.read(ReadWriteConfFile.filepath)
    return cf
  @staticmethod
  def writeConfigParser(cf):
    f=open(ReadWriteConfFile.filepath,"w");
    cf.write(f)
    f.close();
  @staticmethod
  def getSectionValue(section,key):
    cf=ReadWriteConfFile.getConfigParser()
    return cf.get(section, key)
  @staticmethod
  def addSection(section):
    cf=ReadWriteConfFile.getConfigParser()
    allSections=cf.sections()
    if section in allSections:
      return
    else:
      cf.add_section(section)
      ReadWriteConfFile.writeConfigParser(cf)
  @staticmethod
  def setSectionValue(section,key,value):
    cf=ReadWriteConfFile.getConfigParser()
    cf.set(section, key, value)
    ReadWriteConfFile.writeConfigParser(cf)
if __name__ == '__main__':
  ReadWriteConfFile.addSection( 'messages')
  ReadWriteConfFile.setSectionValue( 'messages','name','sophia')
  x=ReadWriteConfFile.getSectionValue( 'messages','1000')
  print x

Under your py script you create an inetMsgConfigure. ini file and test it. If inetMsgConfigure. ini does not exist at all, you can of course call python's method to create a file


file=open('inetMsgConfigure.ini','wb')
file.write(......... Free play )
file.close()

More readers who are interested in Python can check the topics of this site: "Summary of Python Function Use Skills", "Introduction and Advanced Tutorial of Python Object-Oriented Programming", "Python Data Structure and Algorithm Tutorial", "Summary of Python String Operation Skills", "Introduction and Advanced Classic Tutorial of Python" and "Summary of Python File and Directory Operation Skills"

I hope this article is helpful to everyone's Python programming.


Related articles: