簡體   English   中英

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

[英]What is the easiest way in python to modify linux config file?

我有一些配置 linux 計算機的 python 腳本。 其中一項任務是修改配置文件以進行顛覆。 這個文件, ~/.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 ...

我的腳本的任務是找到一個必需的選項,例如store-plaintext-passwords並將其設置為指定值,例如yes 問題是:腳本可以在同一台機器上運行多次,所以如果第一次運行這個選項可以只注釋,如果第二次運行它可以取消注釋並設置為yes ,第三次運行可以指出它未注釋 - 但設置為no等。目前我有一個相當復雜的代碼來搜索字符串文件,將其拆分為注釋/名稱/值,如果需要取消注釋,如果需要更改值並替換它。 也許這是一種更簡單的方法?

~/.subversion/servers 文件是 INI 格式。

所以你可以使用 ConfigParser 來實現你需要的任何東西。

http://docs.python.org/library/configparser.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM