title: Python configparser getting and setting without exceptions
body: <p>Everytime you try to get or set to a section using <code>configparser</code> in Python it throws a <code>NoSectionError</code> if the section does not exist. Is there anyway to avoid this?
  Also, can I also avoid the <code>NoOptionError</code> when getting an option?</p>
  
  <p>For example, using a dictionary, there is the <code>setdefault</code> option: instead of throwing a <code>KeyError</code> when the key does not exist, the dictionary adds the key, sets the key's value to the default value, and returns the default value.</p>
  
  <p>I am currently doing the following for getting attributes:</p>
  
  <pre><code>def read_config(section):
      config = configparser.ConfigParser()
      config.read(location)
      try:
          apple = config.get(section, 'apple')
      except NoSectionError, NoOptionError:
          apple = None
      try:
          pear = config.get(section, 'pear')
      except NoSectionError, NoOptionError:
          pear = None
      try:
          banana = config(section, 'banana')
      except NoSectionError, NoOptionError:
          banana = None
      return apple, pear, banana
  </code></pre>
  
  <p>And the following for setting them:</p>
  
  <pre><code>def save_to_config(section, apple, pear, banana):
      config = configparser.ConfigParser()
      if not os.path.exists(folder_location):
          os.makedirs(folder_location)
  
      config.read(location)
      if section not in config.sections():
          config.add_section(section)
  
      config.set(section, 'apple', apple)
      config.set(section, 'pear', pear)
      config.set(section, 'banana', banana)
  </code></pre>
  
  <p>Setting isn't too bad because they all have the same section, but getting is well... terrible. There has got to be a better way.</p>
  
  <p>Is there perhaps some one liner where I can reduce this:</p>
  
  <pre><code>try:
      apple = config.get(section, 'apple')
  except NoSectionError, NoOptionError:
      apple = None
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>apple = config.get_with_default(section, 'apple', None)
  </code></pre>
  
  <p>-- EDIT --</p>
  
  <p>I have tried to make the following changes per lego's suggestion:</p>
  
  <pre><code>def read_config(section):
      defaults = { section : {'apple': None,
                              'pear': None,
                              'banana': None }} 
      config = configparser.ConfigParser(defaults = defaults)
      config.read(location)
  
      apple = config.get(section, 'apple')
      pear = config.get(section, 'pear')
      banana = config(section, 'banana')
  
      return apple, pear, banana
  </code></pre>
  
  <p>But this still raises a <code>NoSectionError</code> if the section doesn't exist</p>
  
  <p>Note: I have also tried it where defaults = just <code>{'apple': None, 'pear': None, 'banana': None }</code>  (no section)</p>