3

How to edit an INI file in Python 2.7

I am trying to edit an INI config file that already has the Sections and Options I need. However I need to update the values depending on a checkboxlist in wxPython. Currently everything is working :), but I feel like there is a better way. Here is part of the function snippet I'm using.

def read_or_write_file(self, file, section, passed_option = None,
                      value = None, read = True):

    if read:
        with open(file) as configfile:
            config = ConfigParser.RawConfigParser()
            config.readfp(configfile)
            options = config.options(section)

            for option in options:
                file_settings[option] = config.get(section, option)

    else:
        with open(file, 'r+') as configfile:
            config = ConfigParser.RawConfigParser()

            config.readfp(configfile)
            config.set(section, passed_option, value)

        with open(file, 'r+') as configfile:
            config.write(configfile)

This works exactly how I want it to, I tell it what I want to read or write and it works. However the else: part where I write to the file seems strange. I have to edit config first then rewrite everything in the configfile.

Is there a way to only rewrite the value I am changing?

This is my first question, so if I forgot to mention something let me know.

Also a points of information: - I have looked at all of the documentation or at least what I could find - This is similar but not exactly what I need How to read and write INI file with Python3?

2 Answers 2

2

"Is there a way to only rewrite the value I am changing?" No, because it's a text file. You can only do a selective rewrite when you know that the thing you're writing will be exactly the same length as the thing you're replacing. That's not generally the case with text files, and they're almost never treated that way.

I'd do only a small re-organization of this function to remove redundancy:

def read_or_write_file(self, file, section, passed_option = None,
                      value = None, read = True):

    config = ConfigParser.RawConfigParser()
    with open(file) as configfile:
        config.readfp(configfile)    

    if read:
        options = config.options(section)

        for option in options:
            file_settings[option] = config.get(section, option)

    else:
        config.set(section, passed_option, value)

        with open(file, 'w') as configfile:
            config.write(configfile)
Sign up to request clarification or add additional context in comments.

2 Comments

Okay that makes sense, I did not realize that you can't change a text file like that. That would explain why I was having issues getting it to work. I do have another question then. Does the amount of time it takes to rewrite the whole text file really matter? Or should I figure out a different way to implement what I am trying to do?
A typical .ini file is very small, so I wouldn't worry about the time spent writing it out, no.
0

It's not possible in general, because of the way files work. You cannot "insert" bytes into a file - you always overwrite the current content.

It would be possible to rewrite only parts of a file only with the content of the same length, e.g. when you would want to change a string "XXX" to "YYY". But it's quite a common practice to just not worry about it, and serialize such files as a whole every time it's needed.

1 Comment

Thank you for the response! That makes sense, I didn't realize there was no way to insert new information.

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.