Created
October 23, 2014 06:46
-
-
Save andreipak/4e0885e9492101c7140d to your computer and use it in GitHub Desktop.
copy config keys within section using ConfigParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ConfigParser, os | |
config_src = ConfigParser.ConfigParser() | |
config_dst = ConfigParser.ConfigParser() | |
config_keys = "host user passwd name".split() | |
config_keys.reverse() | |
dst_cfg_path = 'cfg1.ini' | |
src_cfg_path = 'cfg2.ini' | |
section = 'db' | |
config_dst.read([os.path.expanduser(dst_cfg_path)]) | |
config_src.read([os.path.expanduser(src_cfg_path)]) | |
for key in config_keys: | |
config_dst.set(section, key, | |
config_src.get(section, key)) | |
out = 'cfg3.ini' | |
with open(out, 'wb') as configfile: | |
config_dst.write(configfile) |
# Copy whole section. Doesn't depend on key names and their order.
for key, value in config_src.items(section):
config_dst.set(section, key, value)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
reference in russian: http://python-lab.ru/documentation/27/stdlib/configparser.html