Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use sys_arg in python config parser

I have my config file in the format given below.

[Connection]
Oracle=Some Value
MySql=Some Value

[Queries]
productionTable1=Somevalue
productionTable2=Somevalue

Now I want to write a program file which would take the connection and table name as input and fetch me the corresponding values from the config file.

I wrote a code like this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import sys
import configparser
config = configparser.ConfigParser()
config.sections()
config.read('mytest.ini')
config.sections()
connection_name=sys.argv[0]
load_query=sys.argv[1]
Myconnection=config.get("Connections","{}".format(connection_name))
My_load_query=config.get("Queries","{}".format(load_query))
print(Myconnection)
print(My_load_query)

But when I run the command "python3 test3.py Oracle productionTable1"

I get the below error:

During handling of the above exception, another exception occurred:

configparser.NoOptionError: No option 'test3.py' in section: 'Connections'

So could anyone help me where I have gone wrong.

>Solution :

As mentioned above, the first argument (sys.argv[0]) is always the name of the file. So you need to use sys.argv[1] and sys.argv[2] to get to your values.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading