使用SafeConfigParser在配置文件中设置选项

1 投票
1 回答
1024 浏览
提问于 2025-04-15 13:44

我正在尝试用Python的ConfigParser对象在我的php.ini文件中设置一个选项(xdebug.profiler_enable)。以下是我想修改的php.ini文件中的部分内容:

[xdebug]
;XDEBUG SETTINGS
;turn on the profiler?
xdebug.profiler_enable=0
xdebug.profiler_append=1    
xdebug.profiler_enable_trigger=0
xdebug.trace_output_name="%R"
xdebug.profiler_output_dir="/home/made_up_user/www/cachegrind/"

Python

import sys
import ConfigParser

if __name__ == '__main__':            
    phpIniLocation = "/home/made_up_user/Desktop/phpinicopy.ini";
    phpIni = ConfigParser.RawConfigParser();
    phpIni.read(phpIniLocation);

    xdebugSetting = phpIni.getboolean("xdebug", "xdebug.profiler_enable");

    if xdebugSetting:
        phpIni.set("xdebug", "xdebug.profiler_enable", "0");                   

    else:
        phpIni.set("xdebug", "xdebug.profiler_enable", "1");

环境: Ubuntu 9.04,Python 2.6 一切看起来都正常。xdebugSetting变量正确返回了选项的布尔值,我可以遍历这个部分并获取每个选项的正确值,而且set方法没有抛出任何异常,但当我检查文件时,选项并没有被更改。我使用了RawConfigParser、ConfigParser和SafeConfigParser,结果都是一样。这个脚本是以root权限运行的。我是不是漏掉了什么?我该如何让set方法正常工作?

1 个回答

2
phpIni.write(open(phpIniLocation, 'w'))

这是一个链接,指向Python的官方文档,具体讲解了ConfigParser这个模块中的RawConfigParser类的write方法。你可以通过这个链接了解更多关于如何使用这个方法的信息。

撰写回答