简体   繁体   English

python 中修改 linux 配置文件的最简单方法是什么?

[英]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.

相关问题 使用Python编辑系统特定配置文件的最简单,最佳方法是什么? - What is the easiest and best way to edit the system specific config file using Python? 在 linux 机器上检测 python 3 中按键的最简单方法是什么? - What is the easiest way to detect key presses in python 3 on a linux machine? 在Linux中从python显示pdf,ps或dvi文件的最简单方法 - Easiest way to display a pdf, ps or dvi file from python in linux 从Python读取FoxPro DBF文件最简单的方法是什么? - What's the easiest way to read a FoxPro DBF file from Python? 在Python中从文件加载数组数据的最简单方法是什么? - what's the easiest way to load array data from a file in Python? 读取由python中的tab分隔的文本文件的最简单方法是什么? - What is the easiest way to read the text file delimited by tab in python? 在 linux 和/或 python 中产生吉他和弦的最简单方法 - Easiest way to produce guitar chords in linux and/or python 使用 python 读取在 scala 中创建的 pureconfig 配置文件的最简单方法是什么? - What is the easiest way to use python to read pureconfig config files created in scala? 在 Python 中转义 HTML 的最简单方法是什么? - What's the easiest way to escape HTML in Python? 用Python制作表格的最简单方法是什么? - What's the easiest way to make a table in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM