#🔒 i cant get Configparser to work

15 messages · Page 1 of 1 (latest)

glass void
#

i have 3 files
Main.py

#!/bin/python3
from CreateConfig import *
CreateConfig()

from GetConfig import *
GetConfig()```

CreateConfig.py
```py
#!/bin/python3

import os
import configparser

def CreateConfig():
    home = os.path.expanduser("~")
    if os.path.exists(f"{home}/.config/config") == True:
        pass
    else:
        config = configparser.ConfigParser()
        config ["Config"] = {
            "idk":"Habicht"}
        with open(f"{home}/.config/config", "w") as ConfigFile:
            config.write(ConfigFile)```
GetConfig.py
```py
#!/bin/python3

import os
import configparser

def GetConfig():
    home = os.path.expanduser("~")
    config = configparser.ConfigParser()
    config.read(f"{home}/.config/config")
    Language = config["idk"]["Habicht"]```

this code fails on the last line in GetConfig.py and i dont understand why

Traceback (most recent call last):
  File "/home/luka/h/Main.py", line 7, in <module>
    GetConfig()
  File "/home/luka/h/GetConfig.py", line 10, in GetConfig
    Language = config["idk"]["Habicht"]
  File "/usr/lib/python3.10/configparser.py", line 965, in __getitem__
    raise KeyError(key)
KeyError: 'idk'
neon sequoiaBOT
#

@glass void

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rotund hare
#

theres no value with the key "idk" in ur config i guess

glass void
#

this is the contents of my config file
[Config]
idk = Habicht

rotund hare
#

huh than it should raise keyword error for Habicht i would think wierd

glass void
#

yeah, i dont know what im doing wrong, i have written this multiple times in different ways but i always get this error

rotund hare
#

oh as i see u should do ["Config"]["idk"] maybe?

glass void
#

thank you, that worked

rotund hare
#

its bc in CreateConfig i said config["Config"] = {"idk": data}
instead of
config["idk"] = data

#

use !solved or !close to close the thread

glass void
#

thank you

#

!close

neon sequoiaBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.