[英]What is the easiest way in python to modify linux config file?
I have some python scripts that configure linux computers.我有一些配置 linux 计算机的 python 脚本。 One of the tasks is to modify a configuration file for subversion.
其中一项任务是修改配置文件以进行颠覆。 This file,
~/.subversion/servers
is very simple and looks like this:这个文件,
~/.subversion/servers
非常简单,看起来像这样:
# store-passwords = no
# store-plaintext-passwords = no
# store-ssl-client-cert-pp = no
# store-ssl-client-cert-pp-plaintext = no
... lots of other options ...
The task of my script is to find a required option, for example store-plaintext-passwords
and to set it to specified value, for example yes
.我的脚本的任务是找到一个必需的选项,例如
store-plaintext-passwords
并将其设置为指定值,例如yes
。 The problem is: the script can run multiple times on same machine, so if it is run first time this option can be just commented, if it is run second time it can be uncommented and set to yes
, third run can point out that it is uncommented - but set to no
etc. Currently i have a rather complex code that search file for the string, splits it for comment/name/value, uncomments it if needed, changes value if needed and replaces it.问题是:脚本可以在同一台机器上运行多次,所以如果第一次运行这个选项可以只注释,如果第二次运行它可以取消注释并设置为
yes
,第三次运行可以指出它未注释 - 但设置为no
等。目前我有一个相当复杂的代码来搜索字符串文件,将其拆分为注释/名称/值,如果需要取消注释,如果需要更改值并替换它。 Maybe it's an easier way possible?也许这是一种更简单的方法?
The ~/.subversion/servers file is in INI format. ~/.subversion/servers 文件是 INI 格式。
So you can use the ConfigParser for implementing whatever you need.所以你可以使用 ConfigParser 来实现你需要的任何东西。
http://docs.python.org/library/configparser.html http://docs.python.org/library/configparser.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.