Sample Header Ad - 728x90

Iterate over keys and use in subprocess in python

0 votes
1 answer
261 views
I have a script in which I have created a function which reads from a config file.
def file_reader():
        config_dict = {}
        configParser = configparser.ConfigParser()

        configParser.read('config.ini')
        for section_name in configParser.sections():
                print('Sections:', section_name)
                print(' Options:', configParser.options(section_name))
                #for (each_key, each_value) in configParser.items(section_name):
                        #config_dict[each_key] = each_value
                config_dict = dict(configParser.items(section_name))
                print(config_dict)
                return config_dict
The output I get is as follows:
Sections: my-config
 Options: ['path1', 'path2', 'path3']
{'path1': '"/home/asad.javed/stop-services.sh"', 'path2': '"/home/asad.javed/script.py"', 'path3': '"/home/asad.javed/getbps.sh ens6f0"'}
My config file looks like this:
cat config.ini
[my-config]
path1 = "/home/asad.javed/stop-services.sh"
path2 = "/home/asad.javed/script.py"
path3 = "/home/asad.javed/getbps.sh ens6f0"
My config file consists of paths that I need to run as external programs. How can I just use the keys, iterate over them and use in subprocess to run external programs sequentially?
Asked by Asad Javed (1 rep)
Sep 16, 2021, 01:44 PM
Last activity: Oct 8, 2021, 02:30 PM