Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在 python 中使用 configparser 模块来读取和写入一些 .ini 样式文件。我希望能够创建和写入该DEFAULTS部分,但是,它似乎是硬编码的,不允许创建这样的部分。
DEFAULTS
可能吗?甚至建议这样做?
您不必创建 DEFAULT 部分,它已经存在。您可以立即在其中设置值。
config = ConfigParser.RawConfigParser() config.set('DEFAULT', 'name2', 'value2') with open('file.conf', 'wb') as cf: config.write(cf)
如 wim 所述,您在创建 ConfigParser 实例时设置为默认值的值也将写入 DEFAULT 部分。