[Baypiggies] Reading k=v format files

Ian Zimmerman itz at buug.org
Sat Oct 3 20:58:07 CEST 2015


I have a file providing settings for both a shell script and a python
program.  It looks like this:

Foo=bar
Foo2=baz

and so on.  I'm looking for a short snippet (I hope for a one liner) to
grok the file and return a dictionary of the settings.  Trivial, you
say?  Yes, but this is not the first time I feel this need, and each
time I find myself rewritting basically the same ditty:

def grok_auth(authfile):
    result=dict()
    with open(authfile) as af:
        for line in af:
            k, v = line.strip().split('=', 1)
            result[k] = v
    return result

There must be a better, batteries based way.  Right?            

-- 
Please *no* private copies of mailing list or newsgroup messages.
Rule 420: All persons more than eight miles high to leave the court.


More information about the Baypiggies mailing list