9

So I am working on creating a program in Python that reads a .ini file to set up some boot variables for the main program. My only thing is, I want the program on initialization, to check if the .ini file exists, and if it doesn't, create it with a set of default values. Kind of a preemptive bug fix on if someone accidentally deletes the file.

I can't seem to find any examples anywhere of how to do this, and I'm not super experienced with Python (only been programming with it for about a week) so I'd appreciate any assistance :)

Edit: Upon further thought, I want to pursue this a bit further.

Let's assume the file does exist. How do I check it to make sure it has the appropriate sections? If it doesn't have the appropriate sections, how would I go about deleting the file or removing the contents and rewriting the contents of the file?

I'm trying to idiot proof this :P

martineau
  • 119,623
  • 25
  • 170
  • 301
Skitzafreak
  • 1,797
  • 7
  • 32
  • 51

1 Answers1

12

You can use ConfigParser and the OS library, here's a quick example:

#!usr/bin/python
import configparser, os

config = configparser.ConfigParser()

# Just a small function to write the file
def write_file():
    config.write(open('config.ini', 'w'))

if not os.path.exists('config.ini'):
    config['testing'] = {'test': '45', 'test2': 'yes'}

    write_file()
else:
    # Read File
    config.read('config.ini')

    # Get the list of sections
    print config.sections()

    # Print value at test2
    print config.get('testing', 'test2')

    # Check if file has section
    try:
        config.get('testing', 'test3')

    # If it doesn't i.e. An exception was raised
    except configparser.NoOptionError:
        print "NO OPTION CALLED TEST 3"

        # Delete this section, you can also use config.remove_option
        # config.remove_section('testing')
        config.remove_option('testing', 'test2')

        write_file()

Output:

[DEFAULT]
test = 45
test2 = yes

Linked above are the docs that are extremely useful to learn more about writing configuration files and other in-built modules.

Note: I'm kind of new to python, so if anyone knows a better approach let me know I'll edit my answer!

Community
  • 1
  • 1
mrhallak
  • 1,138
  • 1
  • 12
  • 27
  • Thank you very much :) Would it make more sense to put the code defining the key:value pairs after the `if not os.path.exists(pathName)` instead of before? That way you don't have the program running that unless the file doesn't exist? – Skitzafreak Jul 04 '17 at 13:50
  • @Skitzafreak sure you can also put both lines 3 and 4 where the parser is defined within the if statement. If I answered your question kindly mark it as correct with the tick button so others know that this question has been answered. – mrhallak Jul 04 '17 at 13:55
  • @Skitzafreak. Anytime! I also edited the answer with the new code. – mrhallak Jul 04 '17 at 13:56
  • Would there be a way for me to check the file if it does exist, and see if it has the proper sections? – Skitzafreak Jul 04 '17 at 14:25
  • @Skitzafreak If I understood your answer correctly, I think you might be looking for this: https://stackoverflow.com/questions/11527939/python-configparser-checking-for-option-existence – mrhallak Jul 04 '17 at 14:27
  • I just editted the OP with the additional info I am looking for – Skitzafreak Jul 04 '17 at 14:31
  • 1
    @Skitzafreak I've edited the answer also please refer to the documentation links I provided earlier – mrhallak Jul 04 '17 at 14:59