1

I have two 'conf' files with content in '[section] name=value' format.

The first conf file has a value that contains the name of a second conf file. I can use ConfigParser.read() to read the first conf file, but when I try to use one of the values read from the first conf file as an input to a second call to ConfigParser.read() I don't get any of the sections from the second conf file returned.

If I hard-code the path to the second config file then everything works as I expect.

So far I have tried this:

    from configparser import ConfigParser
    config = ConfigParser()
    config.read('scripts.conf')
    other_config_file = config['a_section']['other_config_file']
    print(other_config_file) # just to prove I have a valid value
    config.read(other_config_file)
    print(config.sections())

The print(config.sections() above prints JUST the section names from the first config file.

If I do this...

    from configparser import ConfigParser
    config = ConfigParser()
    config.read('scripts.conf')
    other_config_file = config['a_section']['other_config_file']
    print(other_config_file) # just to prove I have a valid value
    config.read('/path/to/second/conf/file.conf')
    print(config.sections())

...the print(config.sections()) prints the section names from BOTH files. In the second code snippet above, the variable other_config_file has the same content as the hard-coded path.

Does anyone have any ideas what I'm doing wrong?

UPDATE: Rather ashamed to admit that I had neglected to notice that the path to the second config file had been quoted in the first config file. The presence of the leading and trailing double quotes invalidated the path.

1
  • 1
    I can't reproduce the issue. With the contents [a_section] other_config_file=scripts2.conf for scripts.conf and [b_section] some_value=10 as contents for scripts2.conf I get the correct output ['a_section', 'b_section']. Could you share the contents of your .conf files (or simplified versions that still show the issue)? Commented Nov 11, 2023 at 14:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.