Python File Handling(.conf) Part-1
What is .conf file?
A CONF file is a configuration or “config” file used on Unix and Linux based systems. It stores settings used to configure system processes and applications. CONF files are similar to .CFG files found on Windows and Macintosh systems.
Most users will not come across a CONF file unless they are looking to update specific settings. If you need to open a CONF file, you can use TextMate in macOS or GNU Emacs in Linux.
Some examples of configuration files include rc.conf for the system startup, syslog.conf for system logging, mongodb.conf for mongoDB settings, smb.conf for the Samba server, and httpd.conf for the Apache Web server.
Getting Started
Pre-requisite:
Install VS Code, Python, Configure VS code to run python application
Create Configuration File:
let’s begin with hardcoding for more clear understanding
Write a small code that reads hardcoded values and print.
Url = “tryftp.codebase.com”
usrName = “codebase”
password = “my#supersecret#password”
print(“\nDisplaying FTP details\n”)
print(“FTP URL: “ + Url)
print(“FTP User Name: “ + usrName)
print(“Password: “ + password)
Output:
Displaying FTP details
FTP URL: tryftp.codebase.com
FTP User Name: codebase
Password: my#supersecret#password
PS D:\Medium\medium code>
Generating the configuration file:
In your code editor create a new file names generate_config.py and import configparser module as shown below,
import configparser
# CREATE OBJECT
config_file = configparser.ConfigParser()
# ADDING SECTION
config_file.add_section(“FTPSettings”)
# CREATING SETTINGS UNDER SECTION
config_file.set(“FTPSettings”, “Url”, “tryftp.codebase.com”)
config_file.set(“FTPSettings”, “uerName”, “codebase”)
config_file.set(“FTPSettings”, “password”, “my#supersecret#password”)
# SAVE CONFIG FILE
with open(r”configurations.conf”, ‘w’) as fileObj:
config_file.write(fileObj)
fileObj.flush()
fileObj.close()
print(“Config file ‘configurations.conf’ created”)
Get Yashraj Santosh Chavan’s stories in your inbox
Join Medium for free to get updates from this writer.
# PRINT FILE CONTENT
read_file = open(“configurations.conf”, “r”)
content = read_file.read()
print(“Content of the config file are:\n”)
print(content)
read_file.flush()
read_file.close()
#flash(): clears internal buffer of the file
If we open the configurations.conf file i.e., the newly generated file, we see the following settings in place,
output:
[FTPSettings]
url = tryftp.codebase.com
uername = codebase
password = my#supersecret#password
Reading the configuration file in Python
Let’s make it a bit organized and reusable. Let’s add a helper file in our VS Code workspace and name it helper.py and add the following code to it,
import configparser
# Method to read config file settings
def read_config():
config = configparser.ConfigParser()
config.read(‘configurations.ini’)
return config
Now this helper method code will serve as reusable method wherever we need the configurations.
Now. Go to the main.py file and import this helper file as
import helper
config = helper.read_config()
ftpUrl = config[‘FTPSetting’][‘ftpUrl’]
userName = config[‘FTPSetting’][‘userName’]
password = config[‘FTPSetting’][‘password’]
print(“\nDisplaying FTP details\n”)
print(“FTP URL: “ + ftpUrl)
print(“FTP User Name: “ + userName)
print(“Password: “ + password)






