I am using configparser to store username and password for a project I am developing. The file Info.ini has the following:
[Username]
user1 = Admin
[Password]
user1 =
[Other]
firsttimeopen = True
The idea here is that, if firsttimeopen = True, then the password the user has inputted is stored instead of validated. Otherwise, the value of ['Password']['User1'] is compared with user input, and user is given access to the rest of the application.
To do this, I tried the following:
configs = configparser.ConfigParser()
with open('Info.ini', 'r') as configfile:
configs.read(configfile)
class verifyLogin():
def __init__(self, user, passw):
self.username = user
self.password = passw
if configs['Other']['firsttimeopen'] == "True":
self.CreatePassword()
else:
self.verifyLoginFunc()
def verifyLoginFunc(self):
if self.password == configs['Password']['User1']:
print("ENTER")
else:
print("NO")
def CreatePassword(self):
configs.set('Password', 'User1', self.password)
configs.set('Other', 'firsttimeopen', 'False')
with open('info.ini', 'w') as configfile:
configs.write(configfile)
However, this throws the following error:
`
Traceback (most recent call last):
File "c:\Users\?\Documents\Programming\Python\IA\LoginGUI.py", line 81, in clicked
verifyLogin(username, password)
File "c:\Users\?\Documents\Programming\Python\IA\LoginGUI.py", line 17, in __init__
if configs['Password']['firsttimeopen'] == "True":
File "C:\Users\?\AppData\Local\Programs\Python\Python310\lib\configparser.py", line 964, in __getitem__
raise KeyError(key)
KeyError: 'Password'
PS: Idk if this is important or not, but to remake/delete the .ini file for testing I have another file test.py:
config = configparser.ConfigParser()
config['Username'] = {'User1': 'Admin'}
config['Password'] = {'User1': ''}
config['Other'] = {'firsttimeopen': "True"}
with open('info.ini', 'w') as configfile:
config.write(configfile)