キーを繰り返してPythonの子プロセスで使用する

キーを繰り返してPythonの子プロセスで使用する

設定ファイルから読み込む関数を作成するスクリプトがあります。

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

私が得た結果は次のとおりです。

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"'}

私の設定ファイルは次のとおりです。

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"

私の設定ファイルには、外部プログラムとして実行する必要があるパスが含まれています。キーのみを使用して外部プログラムを順番に実行し、それを繰り返し、サブプロセスで使用するにはどうすればよいですか?

ベストアンサー1

与えられた辞書の場合、dその値は実行する命令シーケンスに割り当てられる。

import os
d = {}
exitcodes = []
d['path1'] = '/bin/true'
d['path2'] = '/bin/false'
for c in d.values():
  exitcodes.append( os.system(c) )

おすすめ記事