Assume you have a config file (ETLConfig.cfg) with below texts (variables):
[ETL]
etldbname = *****
etllogin = ******
etlpassword = ****
etlport = ****
etlserver = *****
[FPATH]
filepath = D:/ETLFile/
Now we need to use these variable in other python files.
Create a python file (test.py) with below script:
1. Read variables from ETLConfig.cfg using configparser and write those variables to connection strings in your file:
import configparser
import sys
sys.path.append('C:\Python34\Lib\site-packages')
import pprint
import time
import datetime
import os
import csv
import sqlite3
from datetime import date, timedelta
import mysql.connector
from mysql.connector import errorcode
config = configparser.RawConfigParser()
config.read('ETLConfig.cfg')
#GET ETL CONFIG
m1servername = config.get('ETL', 'etlserver')
m1dbname = config.get('ETL', 'etldbname')
m1login = config.get('ETL', 'etllogin')
m1password = config.get('ETL', 'etlpassword')
m1port = config.get('ETL', 'etlport')
cnx = {
'user': m1login,
'password': m1password,
'host': m1servername,
'database': m1dbname,
'raise_on_warnings': True,
'use_pure': False
}
melcnx=mysql.connector.connect(**cnx)
print(melcnx)
#GET ETL FILEPATH CONFIG
etlfilepath = config.get('FPATH', 'filepath')
print(etlfilepath )
2. On executing the python file (test.py), you can see the printed connection strings having same values available in CFG file.
[ETL]
etldbname = *****
etllogin = ******
etlpassword = ****
etlport = ****
etlserver = *****
[FPATH]
filepath = D:/ETLFile/
Now we need to use these variable in other python files.
Create a python file (test.py) with below script:
1. Read variables from ETLConfig.cfg using configparser and write those variables to connection strings in your file:
import configparser
import sys
sys.path.append('C:\Python34\Lib\site-packages')
import pprint
import time
import datetime
import os
import csv
import sqlite3
from datetime import date, timedelta
import mysql.connector
from mysql.connector import errorcode
config = configparser.RawConfigParser()
config.read('ETLConfig.cfg')
#GET ETL CONFIG
m1servername = config.get('ETL', 'etlserver')
m1dbname = config.get('ETL', 'etldbname')
m1login = config.get('ETL', 'etllogin')
m1password = config.get('ETL', 'etlpassword')
m1port = config.get('ETL', 'etlport')
cnx = {
'user': m1login,
'password': m1password,
'host': m1servername,
'database': m1dbname,
'raise_on_warnings': True,
'use_pure': False
}
melcnx=mysql.connector.connect(**cnx)
print(melcnx)
#GET ETL FILEPATH CONFIG
etlfilepath = config.get('FPATH', 'filepath')
print(etlfilepath )
2. On executing the python file (test.py), you can see the printed connection strings having same values available in CFG file.
No comments:
Post a Comment