-1

Is there a way to append the values to config file using configparser?

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
message = first run, second run, third run

I want to append message value like shown below

message = first run, second run, third run, fourth run.

Is that possible, can anyone help?

colidyre
  • 4,170
  • 12
  • 37
  • 53
  • I haven't downvoted, but it would be good to show what you have tried so far. – colidyre Apr 07 '20 at 12:06
  • I'm still looking how to append reading the config parser document – Eager2Learn Apr 07 '20 at 12:08
  • You should still include what you have tried. You don't learn if others write your code for you. Have a look at the [configparser docs](https://docs.python.org/3.8/library/configparser.html) - there are some pretty good examples there. – Badgy Apr 07 '20 at 12:10
  • import configparser config = configparser.ConfigParser() file_name = config.read('config.ini') config['DEFAULT']['message'] = 'fourth run' with open(file_name, 'a') as configfile: config.write(file_name) print(config['DEFAULT']['message']) – Eager2Learn Apr 07 '20 at 12:17
  • @Eager2Learn The only problem with that is you are *replacing* the `message` value rather than appending to it. You also don't want to append to the file, as that will cause a second copy of the configuration to be written to the existing file. – chepner Apr 07 '20 at 12:18
  • import configparser config = configparser.ConfigParser() file_name = config.read('config.ini') config['DEFAULT']['message'] = 'fourth run' with open(file_name, 'a') as configfile: config.write(file_name) print(config['DEFAULT']['message']) I'm trying this – Eager2Learn Apr 07 '20 at 12:18
  • I want to append the message value – Eager2Learn Apr 07 '20 at 12:19
  • Please avoid posting code in the comments like this. It is very hard to read. You can [edit] the question to include any updates you have – Tomerikoo Apr 07 '20 at 12:20

2 Answers2

1

Note that configparser doesn't implement a file editor; it reads a configuration from a file, and it writes a configuration to a file. Any changes to the configuration are made in memory until the entire configuration is written out again.

# Read into memory
config = configparser.ConfigParser()
with open("config.ini") as f:
    config.read_file(f)

# Update the in-memory configuration
config["DEFAULT"]["message"] +=", fourth run"

# Write configuration to disk
# WARNING: You should write to a temporary file first,
# so that you can safely replace config.ini after
# the write succeeds.
with open("config.ini", "w") as f:
     config.write(f)
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    For what that's worth, `ConfigParser.set` and `ConfigParser.get` are [legacy methods](https://docs.python.org/3/library/configparser.html#legacy-api-examples), at least for the usage here. The config object can just be treated as a nested dict e.g. `config['DEFAULT']['message'] += ', fourth run'`. Also you can just give the filename to `config.read`, you only need a "proper" `open` to write back. – Masklinn Apr 07 '20 at 12:13
  • Thanks; I rarely use `configparser` myself, and just did a quick skim to see how to actually update the configuration. – chepner Apr 07 '20 at 12:16
1

Is that possible, can anyone help?

Yes, just parse your file, concatenate the additional bits to the entry you want to modify, and write it back?

ConfigParser has no structure beyond sections and items, so entries are just arbitrary string data. If you want something more structured (e.g. have entries be arrays for instance) you should use something like toml, then you could append individual items to the entry instead of having to do string-wise concatenation.

Masklinn
  • 34,759
  • 3
  • 38
  • 57