0

I have a original config file which I found a very nice way to read. Like this:

myDict = {}
cfg = file(configFile, 'r').readlines()
myDict = eval('\n'.join(cfg))

This gets my file's contents in to myDict very nicely. But what is a simple looking way to make changes to its contents and create/write to a new modified file? thanks

golu
  • 629
  • 2
  • 7
  • 18
  • 5
    Nooo.... don't use `eval(..)`... Someone can simply hack arbitrary code into your program now... – Willem Van Onsem Jul 12 '17 at 21:19
  • 2
    @WillemVanOnsem Thanks for cringing on my behalf. – cs95 Jul 12 '17 at 21:19
  • If you're anyway joining them with a newline, you can just call `f.read()`. Also, what's `file(...)`? Did you mean `open(...)`? – cs95 Jul 12 '17 at 21:20
  • 1
    You might as well just use JSON for this. – juanpa.arrivillaga Jul 12 '17 at 21:21
  • 1
    @cᴏʟᴅsᴘᴇᴇᴅ old, deprecated Python 2 builtin that works like `open` : https://docs.python.org/2/library/functions.html#file I only ever used it to check `isinstance(obj, file)`. – juanpa.arrivillaga Jul 12 '17 at 21:22
  • Possible duplicate of [Parsing values from a JSON file using Python?](https://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-using-python) – cs95 Jul 12 '17 at 21:22
  • 2
    `ast.literal_eval` (https://docs.python.org/2/library/ast.html#ast.literal_eval) should be able to handle legitimate uses of this kind of pattern without the dangers `eval` introduces. Or, you know, use json or yaml or https://docs.python.org/2/library/configparser.html or something. – Peter DeGlopper Jul 12 '17 at 21:22
  • @juanpa.arrivillaga Ah, I see. Had no idea about it. Cool... – cs95 Jul 12 '17 at 21:24
  • sounds like no one like eval. I'll have a look into json. But what making changes to dict and writing it back to a file? – golu Jul 12 '17 at 21:27
  • If you want to make your config 'human readable/editable', use the built-in `configparser` module, or YAML (`ruamel.yaml` should do just fine). Otherwise JSON is more than enough and you can easily parse it with `your_dict = json.read(your_file_handle)` and write to it with `json.write(your_file_handle, dict)`. – zwer Jul 12 '17 at 21:32
  • @golu `dicts` are compatible with JSON, as long as you use lists and dicts only. Look into `json.load` and `json.dump`. – cs95 Jul 12 '17 at 21:34
  • Thanks, but config already exists and changing it's format is not an option. I can only read it, make changes and save it (in same format) to a new file – golu Jul 12 '17 at 21:34
  • @golu - does your config have complex values (i.e. nested dictionaries and such)? If not, `configparser` will still work on it just fine. – zwer Jul 12 '17 at 21:36
  • Yes it is. Here's a small sample: https://ibb.co/eWHsXv. I will look into configparser – golu Jul 12 '17 at 21:42
  • configparser didn't work --> Error: MissingSectionHeaderError: File contains no section headers – golu Jul 12 '17 at 21:49

1 Answers1

-2

You could try this (overlooking the security hole of eval):

myDict = {}
with open(configFile, 'r+') as file:
    cfg = file.readlines()
    myDict = eval('\n'.join(dgf))
    myDict['new key'] = 'new value'

    file.seek(0)
    file.write(str(myDict)) #format this if you want, str() is raw
    file.truncate()

The above should open the file, evaluate the contents as a dict (as per your code), add a new value (just modifying the dictionary), and then overwrite the original file.

Jack
  • 380
  • 1
  • 3
  • 12