Write a function that parses `key=value` lines (like a .env file) into a dict, ignoring comments.

BeginnerHands-onPython for DevOps
Answer
def parse_env(path):
    env = {}
    with open(path) as f:
        for raw in f:
            line = raw.strip()
            if not line or line.startswith("#") or "=" not in line:
                continue
            key, _, value = line.partition("=")
            env[key.strip()] = value.strip().strip('"').strip("'")
    return env

Key trick → partition("=") splits only on the first =, so values like DB_URL=postgres://u:p@h/db?sslmode=require stay intact.

All Real-World Interview questions Roadmap: Interview readiness

More on Python for DevOps