0

I have made a program that collects and creates data inside config files (.ini) but the thing is I need to know how I can make it so it checks if something is already in the config file and then if it is not in there it will right it into there.

Here is kind of what I want it to look like:

if 'name' not in config:
    config['name']
else:
    print('Name already exists.')

so any Ideas?

Thanks In Advance!

Dan Alexander
  • 2,004
  • 6
  • 24
  • 34

2 Answers2

0

Here's a sample:

import os

# File does not exist, let's create it
if os.path.exists('name') == False:
    f = open('name', 'w')
    f.write('sometext')
    f.close()

# File exists, let's check if it's empty and write into it
# or if there's some text, write nothing
else:
    f = open('name', 'rw')
    prev_text = f.read()

    # File was empty
    if prev_text == '':
        f.write('sometext')
    # File already had some text
    else:
        print 'This name already exists'

    f.close()
0

Well I fixed it by doing this:

if user in config:
    print('User Taken!')
else:
config.add_section(user)

That sorted it all out, but thanks for the help anyway!

Dan Alexander
  • 2,004
  • 6
  • 24
  • 34