【问题标题】:setting option in config file using SafeConfigParser使用 SafeConfigParser 在配置文件中设置选项
【发布时间】:2009-08-20 02:48:11
【问题描述】:

我正在尝试使用 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,蟒蛇 2.6 一切似乎都运行良好。 xdebugSetting 变量正确返回选项的布尔值,我可以解析该部分并检索每个选项的正确值,并且 set 方法不会引发任何异常,但是当我检查文件时,选项没有被更改。我使用了 RawConfigParser、ConfigParser 和 SafeConfigParser,结果都一样。该脚本以 root 权限运行。有什么我想念的吗?如何让 set 方法起作用?

【问题讨论】:

  • 是什么让您认为配置解析器会更新配置文件?你在哪里读到的?

标签: python linux file-io ubuntu


【解决方案1】:
phpIni.write(open(phpIniLocation, 'w'))

docs.

【讨论】:

  • 谢谢,我应该更彻底地阅读文档。
  • @unknown (google):如果这是正确的答案,请不要说“谢谢”(虽然,这是件好事)。点击绿色的“接受这个答案”复选标记(这样更好。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
  • 1970-01-01
相关资源
最近更新 更多