Configuration Files in Python
Configuration Files in Python
Configuration files popularly called config files are special files that store some specific data and
settings for computer programs. Most computer programs read their configuration files at startup
and periodically check for changes in these configuration files.
The files can be used by the user to change the settings of the application without the need to
recompile the programs. Generally each configuration file consists of different sections. Each
section contains key and value pairs like a Python dictionary.
Given below is a sample configuration file which consists of three sections namely Address,
Education, and the Hobbies of a person.
[Address]
Name = Aditya Raj
Village = Bhojpur
District = Samastipur
State = Bihar
[Education]
College=IIITA
Branch= IT
[Favorites]
Sport = VolleyBall
Book = Historical Books
Now we will create the above configuration file using the ConfigParser module in python.
#import module
import configparser
#create configparser object
config_file = configparser.ConfigParser()
#define sections and their key and value pairs
config_file["Address"]={
"Name": "Aditya Raj",
"Village": "Bhojpur",
"District": "Samastipur",
"State": "Bihar"
}
config_file["Education"]={
"College":"IIITA",
"Branch" : "IT"
}
config_file["Favorites"]={
"Sports": "VolleyBall",
"Books": "Historical Books"
}
#SAVE CONFIG FILE
with open("person.ini","w") as file_object:
config_file.write(file_object)
print("Config file 'person.ini' created")
#print file content
read_file=open("person.ini","r")
content=read_file.read()
print("content of the config file is:")
print(content)
Here in the example below, we will add a new section “Physique” in the person.ini file which
already contains the Address, Education and Favorites sections.
import configparser
#print initial file content
read_file=open("person.ini","r")
content=read_file.read()
print("content of the config file is:")
print(content)
#create new config object
config_object= configparser.ConfigParser()
#read config file into object
config_object.read("person.ini")
#Add new section named Physique
config_object["Physique"]={
"Height": "183 CM",
"Weight": "70 Kg"
}
#save the config object back to file
with open("person.ini","w") as file_object:
config_object.write(file_object)
#print the new config file
print("Config file 'person.ini' updated")
print("Updated file content is:")
nread_file=open("person.ini","r")
ncontent=nread_file.read()
print(ncontent)
We can also use add_section() method to add a new section and then use set() method to add
new fields in the section.
import configparser
#print initial file content
read_file=open("person.ini","r")
content=read_file.read()
print("content of the config file is:")
print(content)
#create new config object
config_object= configparser.ConfigParser()
#read config file into object
config_object.read("person.ini")
#Add new section named Physique
config_object.add_section('Physique')
config_object.set('Physique', 'Height', '183 CM')
config_object.set('Physique', 'Weight', '70 Kg')
#save the config object back to file
with open("person.ini","w") as file_object:
config_object.write(file_object)
#print the updated config file
print("Config file 'person.ini' updated")
print("Updated file content is:")
nread_file=open("person.ini","r")
ncontent=nread_file.read()
print(ncontent)
Output:
In the above example, we can see that add_section() method takes section name as it’s
argument while set() method takes section name as it’s first argument,field name as it’s second
argument and value for field as it’s third argument.
These two methods can also be used while making a new config file to add sections and
fields to the file instead of using dictionaries as we have done in this example.
In the following code we have added a new field “Year” in “Education” section of person.ini
config file and modified the value of “Branch” field in the file.
import configparser
#print initial file content
read_file=open("person.ini","r")
content=read_file.read()
print("content of the config file is:")
print(content)
#create new config object
config_object= configparser.ConfigParser()
#read config file into object
config_object.read("person.ini")
#update value of a field in a section
config_object["Education"]["Branch"]="MBA"
#add a new field in a section
config_object["Education"].update({"Year":"Final"})
#save the config object back to file
with open("person.ini","w") as file_object:
config_object.write(file_object)
#print updated content
print("Config file 'person.ini' updated")
print("Updated file content is:")
nread_file=open("person.ini","r")
ncontent=nread_file.read()
print(ncontent)
Output:
In the above example, we can use update() method to add new fields as well as modify existing
fields. If the field given as argument exists in the file, it updates the field otherwise a new field is
created.
import configparser
#print initial file content
read_file=open("person.ini","r")
content=read_file.read()
print("content of the config file is:")
print(content)
#create new config object
config_object= configparser.ConfigParser()
#read config file into object
config_object.read("person.ini")
#delete a field in a section
config_object.remove_option('Education', 'Year')
#delete a section
config_object.remove_section('Physique')
#save the config object back to file
with open("person.ini","w") as file_object:
config_object.write(file_object)
#print new config file
print("Config file 'person.ini' updated")
print("Updated file content is:")
nread_file=open("person.ini","r")
ncontent=nread_file.read()
print(ncontent)
Output:
In the above example, we can see that remove_option() method takes section name as it’s first
argument and field name as it’s the second argument whereas remove_section() method takes
the name of the section to be deleted as its argument.