How to Create Configuration Files With Constant Parameters Using Python?

How to Create Configuration Files With Constant Parameters Using Python?

Hi Emma,

Configuration files contain a structured set of variables, organized in key/value pairs. Configuration files typically have different extensions like .ini, .cnf, .conf, .cfg and .cf. There are several modules and techniques to create and parse them in python.

Here are some of the commonly used configuration files in Python:

  • Python Configuration Files
  • JSON Configuration Files
  • YAML Configuration Files
  • INI Configuration Files
  • XML Configuration Files

Despite the fact that Python files are not used to store configuration information, one of the greatest advantages of this approach is the ease in the management of configuration variables. For example, to store these configurations, we can create a config.py file.

drivers_config = {

    "URL": "localhost.com",
    "FileName": "name.json",
}

# implementation comes here ...

You can use this config file in the actual implementation as shown below:

import config as cfg

driver = webdriver.Firefox()

driver.get(cfg.drivers_config["URL"])

As seen above, the config file is imported into the code before using the configuration parameters present in it.

Hope it helps!

1 Like