简体   繁体   中英

python configparser map section values to other sections

I'd like to be able to read a variable list of files from a config file section and use the keys from that list to point to further sections that define attributes for each file. The files can change in number and name and attributes. Has anyone seen any method of doing this or can point me in the right direction?

[paths]
file1=/some/path/
file2=/some/other/path

[file1]
key_specific_to_file_1=some_attribute_value

[file2]
key_specific_to_file_2=some_attribute_value2

[non-file-related-section]
some_key=some-other-value

there is standard module configparser. documentation can be found configparser documentation

simple example :



    #/usr/bin/env python
    import configparser
    import sys

    def config_reader(filename):
        try:
            config = configparser.ConfigParser()
            config.read(filename )
            section_list = config.sections()
            for section_name in section_list:
                for key in config[section_name]:
                    print("Key : " +  config[section_name ][key])
        except configparser.Error as e:
            print(e)
            return 

    def main():
        print("config file " + sys.argv[1])
        config_reader(sys.argv[1])


    if __name__ == "__main__":
        main()


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM