Working with INI Configuration Files Using configparser

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Many applications can use configuration files to read configuration options and other parameters that finish the start up of the application, or make it behave in a certain way. Configuration files can also be used to define server setup, and keep key parameters that allow the application to run with minimum user input. In this lab we will explore the reading, writing, and updating of configuration files using Python’s `configparser`.

You will need basic Python programming and SQL skills for this lab:
– [Certified Associate in Python Programming Certification](https://linuxacademy.com/cp/modules/view/id/470)
– [configparser Documentation](https://docs.python.org/3/library/configparser.html)

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the printer_config.ini File

Create printer_config.ini using configparser.

Run python printer_file.py. This will result in an AssertionError.

Now update printer_file.py so that is passes the test.

# data to be used
# ["author", "title", "pages", "due_date", "genre", "isbn"],
data_list = [
    ["Thompson, Keith", "Oh Python! My Python!", 1200, "2020-11-15", "biography", "000-1-000000-00-1"],
    ["Fritts, Larry", "Fun with Django", 150, "2020-06-23", "satire", "00-2-000000-00-2"]
]

novel_text = "Numquam labore labore tempora. Dolore dolorem quisquam aliquam eius dolor non ipsum. Dolorem est velit tempora quaerat. Est magnam porro voluptatem dolore. Adipisci voluptatem consectetur dolore voluptatem voluptatem neque. Adipisci non modi quiquia etincidunt quisquam. Etincidunt consectetur dolore ut. Quaerat ut adipisci sit aliquam quisquam ut. Voluptatem labore porro porro.nnPorro modi numquam non sed dolor. Consectetur dolor adipisci quaerat aliquam adipisci. Etincidunt porro modi dolor neque numquam magnam neque. Ipsum consectetur ut eius ut. Dolor ut modi non est tempora labore sit.nnEtincidunt voluptatem quaerat consectetur sed est etincidunt ipsum. Ut adipisci numquam etincidunt porro porro. Dolore eius modi aliquam. Velit amet sed quaerat etincidunt est. Ut consectetur modi ipsum sed aliquam. Amet tempora quiquia eius voluptatem aliquam dolore ut. Etincidunt dolor non voluptatem eius sed non adipisci."

# use configparser to create the configuration file as described in the instructions
# write the file to printer_config.ini
config = configparser.ConfigParser()

config["DEFAULT"] = {
    "pub_house": "Atlantic Publishing",
    "number_of_books": '2'
}

for index, book in enumerate(data_list):
    config[f"book{index + 1}"] = {
        "author": book[0],
        "title": book[1],
        "genre": book[4],
        "text": novel_text
    }

with open("printer_config.ini", "w") as f:
    config.write(f)

Run python printer_file.py.

Congratulations! You have shown that you can use configparser to write a configuration file.

Update the printer_config.ini File to Include ISBN

The publisher apologized because they forgot to ask for the ISBN of each book. Update the printer_config.ini file to include the ISBN for each book.

Run python update_printer_file.py. This will also result in an AssertionError.

Now update update_printer_file.py so that is passes the test.

# data to be used, removed field names from list
# ["author", "title", "pages", "due_date", "genre", "isbn"],
data_list = [
    ["Thompson, Keith", "Oh Python! My Python!", 1200, "2020-11-15", "biography", "000-1-000000-00-1"],
    ["Fritts, Larry", "Fun with Django", 150, "2020-06-23", "satire", "00-2-000000-00-2"]
]

novel_text = "Numquam labore labore tempora. Dolore dolorem quisquam aliquam eius dolor non ipsum. Dolorem est velit tempora quaerat. Est magnam porro voluptatem dolore. Adipisci voluptatem consectetur dolore voluptatem voluptatem neque. Adipisci non modi quiquia etincidunt quisquam. Etincidunt consectetur dolore ut. Quaerat ut adipisci sit aliquam quisquam ut. Voluptatem labore porro porro.nnPorro modi numquam non sed dolor. Consectetur dolor adipisci quaerat aliquam adipisci. Etincidunt porro modi dolor neque numquam magnam neque. Ipsum consectetur ut eius ut. Dolor ut modi non est tempora labore sit.nnEtincidunt voluptatem quaerat consectetur sed est etincidunt ipsum. Ut adipisci numquam etincidunt porro porro. Dolore eius modi aliquam. Velit amet sed quaerat etincidunt est. Ut consectetur modi ipsum sed aliquam. Amet tempora quiquia eius voluptatem aliquam dolore ut. Etincidunt dolor non voluptatem eius sed non adipisci."

# use configparser to change the configuration file as described in the instructions
# overwrite printer_config.ini
config = configparser.ConfigParser()
config.read('printer_config.ini')

for index, book in enumerate(data_list):
    config.set(f"book{index+1}", "isbn", book[5])

with open('printer_config.ini', 'w') as f:
    config.write(f)

Run python update_printer_file.py.

Congratulations! You have shown that you can use configparser to update a configuration file.

Additional Resources

Atlantic Publishing has its first two books ready to be published. The publisher has requested the information be sent in the following format, in a file named printer_config.ini:

[DEFAULT]
pub_house = Atlantic Publishing
n
number_of_books = 2

[book#]
author = Thompson, Keith
title = Oh Python! My Python!
genre = biography
text = Numquam labore labore tempora. Dolore dolorem quisquam aliquam eius dolor non ipsum. Dolorem est velit tempora quaerat. Est magnam porro voluptatem dolore. Adipisci voluptatem consectetur dolore voluptatem voluptatem neque. Adipisci non modi quiquia etincidunt quisquam. Etincidunt consectetur dolore ut. Quaerat ut adipisci sit aliquam quisquam ut. Voluptatem labore porro porro.
        Porro modi numquam non sed dolor. Consectetur dolor adipisci quaerat aliquam adipisci. Etincidunt porro modi dolor neque numquam magnam neque. Ipsum consectetur ut eius ut. Dolor ut modi non est tempora labore sit.
        Etincidunt voluptatem quaerat consectetur sed est etincidunt ipsum. Ut adipisci numquam etincidunt porro porro. Dolore eius modi aliquam. Velit amet sed quaerat etincidunt est. Ut consectetur modi ipsum sed aliquam. Amet tempora quiquia eius voluptatem aliquam dolore ut. Etincidunt dolor non voluptatem eius sed non adipisci.

...

The two books to be published are "Oh Python! My Python" and "Fun with Django." You have pulled up the relevant information for those two books from the database here:

data_list = [
    ["author", "title", "pages", "due_date", "genre", "isbn"],
    ["Thompson, Keith", "Oh Python! My Python!", 1200, "2020-11-15", "biography", "000-1-000000-00-1"],
    ["Fritts, Larry", "Fun with Django", 150, "2020-06-23", "satire", "00-2-000000-00-2"]
]

novel_text = "Numquam labore labore tempora. Dolore dolorem quisquam aliquam eius dolor non ipsum. Dolorem est velit tempora quaerat. Est magnam porro voluptatem dolore. Adipisci voluptatem consectetur dolore voluptatem voluptatem neque. Adipisci non modi quiquia etincidunt quisquam. Etincidunt consectetur dolore ut. Quaerat ut adipisci sit aliquam quisquam ut. Voluptatem labore porro porro.nnPorro modi numquam non sed dolor. Consectetur dolor adipisci quaerat aliquam adipisci. Etincidunt porro modi dolor neque numquam magnam neque. Ipsum consectetur ut eius ut. Dolor ut modi non est tempora labore sit.nnEtincidunt voluptatem quaerat consectetur sed est etincidunt ipsum. Ut adipisci numquam etincidunt porro porro. Dolore eius modi aliquam. Velit amet sed quaerat etincidunt est. Ut consectetur modi ipsum sed aliquam. Amet tempora quiquia eius voluptatem aliquam dolore ut. Etincidunt dolor non voluptatem eius sed non adipisci."

Note: In a never-before-seen occurrence, both novels have exactly the same text!

Logging In

There are a couple of ways to get in and work with the code. One is to use the credentials provided in the hands-on lab page, log in with SSH, and use a text editor in the terminal.

The other is using VS Code in the browser. If you’d like to go this route, then you will need to navigate to the public IP address of the workstation server (provided in the hands-on lab overview page) on port 8080 (example: http://PUBLIC_IP:8080). Your password will be the same password that you'd use to connect over SSH.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?