grigoryvp
grigoryvp

Reputation: 42513

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

I have some python scripts that configure linux computers. One of the tasks is to modify a configuration file for subversion. This file, ~/.subversion/servers is very simple and looks like this:

# 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. 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. Maybe it's an easier way possible?

Upvotes: 0

Views: 2791

Answers (1)

user2665694
user2665694

Reputation:

The ~/.subversion/servers file is in INI format.

So you can use the ConfigParser for implementing whatever you need.

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

Upvotes: 3

Related Questions