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 9062201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:40:49+00:00 2026-06-16T15:40:49+00:00

Simple question. It is possible to make configobj to not put a space before

  • 0

Simple question. It is possible to make configobj to not put a space before and after the ‘=’ in a configuration entry ?

I’m using configobj to read and write a file that is later processed by a bash script, so putting an antry like:

VARIABLE = “value”

breaks the bash script, it needs to always be:

VARIABLE=”value”

Or if someone has another suggestion about how to read and write a file with this kind of entries (and restrictions) is fine too.

Thanks

  • 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-06-16T15:40:50+00:00Added an answer on June 16, 2026 at 3:40 pm

    Well, as suggested, I ended up writing my own parser for this that can be used exactly in the same way as ConfigObj:

    config = MyConfigParser("configuration_file")
    print config["CONFIG_OPTION_1"]  
    config["CONFIG_OPTION_1"]= "Value 1"
    print config["CONFIG_OPTION_1
    config.write()
    

    This is the code if someone is interested or wants to give suggestions (I started coding in python not so long ago so probably there are lots of room for improvement). It respects the comments and the order of the options in the file, and correctly scapes and adds double quotes where needed:

    import os
    import sys
    
    class MyConfigParser:
      name = 'MyConfigParser'
      debug = False
      fileName = None
      fileContents = None
      configOptions = dict()  
    
      def __init__(self, fileName, debug=False):
        self.fileName = fileName
        self.debug = debug    
        self._open()
    
      def _open(self):       
        try:
            with open(self.fileName, 'r') as file:
        for line in file:
          #If it isn't a comment get the variable and value and put it on a dict
          if not line.startswith("#") and len(line) > 1:
        (key, val) = line.rstrip('\n').split('=')
        val = val.strip()
        val = val.strip('\"')
        val = val.strip('\'')
        self.configOptions[key.strip()] = val
    except:
      print "ERROR: File "  + self.fileName + " Not Found\n"
    
      def write(self):
    try:
      #Write the file contents
      with open(self.fileName, 'r+') as file:
        lines = file.readlines()
        #Truncate file so we don't need to close it and open it again 
        #for writing
        file.seek(0)
        file.truncate()      
    
        i = 0
        #Loop through the file to change with new values in dict      
        for line in lines:    
          if not line.startswith("#") and len(line) > 1:
        (key, val) = line.rstrip('\n').split('=')
        try:
          if key in line:
            newVal = self.configOptions[key]
            #Only update if the variable value has changed
            if val != newVal:
              newLine = key + "=\"" + newVal + "\"\n"
              line = newLine
        except:
          continue
          i +=1
          file.write(line)
    except IOError as e:
      print "ERROR opening file " + self.fileName + ": " + e.strerror + "\n"
    
    
      #Redefinition of __getitem__ and __setitem__
    
      def __getitem__(self, key):  
    try:
      return self.configOptions.__getitem__(key)
    except KeyError as e:
      if isinstance(key,int):
        keys = self.configOptions.keys()
        return self.configOptions[keys[key]]
      else:
        raise KeyError("Key " +key+ " doesn't exist")
    
      def __setitem__(self,key,value):
    self.configOptions[key] = value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Hopefully) Simple question: Is it possible to make the jQuery UI Datepicker draggable? If
I happen to be using innodb, read-committed. My simple question is this relative to
Very simple question, is it possible to make a variable that is retrieved from
A simple question: Is it possible to view gui of java Swing library and
I have a very simple question: Is it possible to change the price of
I have a simple question Is it possible to download a specific resource file
I have a (hopefully) simple question. Is it possible to use SSHD tunneling to
This is a really simple question. Is it possible for jQuery to get an
I've only have a simple question to ask : Is it possible to call
this may be a very simple question, but is it possible to force a

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.