Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:20:25+00:00 2026-05-28T19:20:25+00:00

Ok i am planning on using the configparser module for creation of a few

  • 0

Ok i am planning on using the configparser module for creation of a few ini configuration files, I have ran into some trouble though.

How can i write certain sections of my config to different ini files?

I have read all through the official docs on it, and don’t seem to see anything that allows this to be done.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:20:26+00:00Added an answer on May 28, 2026 at 7:20 pm

    You could define a subclass of one of the ConfigParser classes and override its write method. For example:

    class MySafeConfigParser(ConfigParser.SafeConfigParser):
        def write(self, fileobject, *sections):
            'Write an .ini-format representation of the specified sections'
            temp = ConfigParser.SafeConfigParser(self.defaults())
            for s in sections:
                temp.add_section(s)
                for i, v in self.items(s):
                    temp.set(s, i, v)
            temp.write(fileobject)
    

    I can test my subclass with something like this:

    config = MySafeConfigParser({ 'foo': '5.0' })
    
    config.add_section('Section1')
    config.set('Section1', 'foo', '3.1415')
    config.set('Section1', 'bar', 'baz')
    
    config.add_section('Section2')
    config.set('Section2', 'abc', 'def')
    config.set('Section2', 'ghi', 'jkl')
    
    config.add_section('Section3')
    config.set('Section3', 'mno', 'pqr')
    config.set('Section3', 'stu', 'vwx')
    
    with open('section1.ini', 'wb') as f:
        config.write(f, 'Section1')
    
    with open('sections2and3.ini', 'wb') as f:
        config.write(f, 'Section2', 'Section3')
    

    After I run this code, the file section1.ini contains:

    [DEFAULT]
    foo = 5.0
    
    [Section1]
    foo = 3.1415
    bar = baz
    

    and sections2and3.ini contains:

    [DEFAULT]
    foo = 5.0
    
    [Section2]
    foo = 5.0
    abc = def
    ghi = jkl
    
    [Section3]
    foo = 5.0
    mno = pqr
    stu = vwx
    

    And best of all, the ConfigParser.read(...) method already accepts multiple filenames, so I can re-read both configuration files with:

    config = MySafeConfigParser()
    config.read(['section1.ini', 'sections2and3.ini'])
    

    Edit: I missed that the original question was asking about Python 3, so here’s the above code converted to Python 3:

    class MySafeConfigParser(configparser.ConfigParser):
        def write(self, fileobject, *sections, space_around_delimiters = True):
            'Write an .ini-format representation of the specified sections'
            temp = configparser.ConfigParser(self.defaults())
            for s in sections:
                temp.add_section(s)
                for i, v in self.items(s):
                    temp.set(s, i, v)
            temp.write(fileobject, space_around_delimiters)
    

    And the test code converted to Python 3:

    config = MySafeConfigParser({ 'foo': '5.0' })
    
    config['Section1'] = {}
    config['Section1']['foo'] = '3.1415'
    config['Section1']['bar'] = 'baz'
    
    config['Section2'] = {}
    config['Section2']['abc'] = 'def'
    config['Section2']['ghi'] = 'jkl'
    
    config['Section3'] = {}
    config['Section3']['mno'] = 'pqr'
    config['Section3']['stu'] = 'vwx'
    
    with open('section1.ini', 'w') as f:
        config.write(f, 'Section1')
    
    with open('sections2and3.ini', 'w') as f:
        config.write(f, 'Section2', 'Section3')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm planning on using MEF within ASP.NET looking for some insight into thread safety
Background: I'm planning on using the JW FLV Media Player for streaming some videos:
I'm planning on caching some information using ehcache. Normally, I would follow the pattern:
Am planning on using declarative authorization in a Rails 3 app. I have the
Am planning on using declarative authorization in a Rails 3 app. I have the
I've been planning on using GIT for version control. However, I have a prolly
We are planning on using Red Gate SQL Source Control , but have not
Planning of using t4 template in a production environment. I have not used this
Im planning on using ASIHTTP to handle some long file downloads/installs and I'd like
I have the following scenario where I’m planning in using windows authentication. 1.1) I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.