Как я могу проверить, есть ли в главном файле ConfigParser раздел,
А если нет, то добавить в него новый раздел?
Я пытаюсь использовать этот код:
import ConfigParser
config = ConfigParser.ConfigParser()
#if the config file don´t has the section 'globel', then add it to the config-file
if not config.has_section("globel"):
config.add_section("globel")
config.set("globel", "settings", "someting")
config.write(open("/tmp/myfile.conf", "w"))
print "The new sestion globel is now added!!"
#else just return the the value of globle->settings
else:
config.read("/tmp/myfile.conf")
print "else... globle->settings == "+config.get("globel", "settings")
Вам необходимо прочитать файл конфигурации перед проверкой раздела. Поместите строку
config.read("/tmp/myfile.conf")
сразу после
config = ConfigParser.ConfigParser()
Нет ошибки, если файл конфигурации еще не существует.