Skip to content

Instantly share code, notes, and snippets.

@winebarrel
Last active December 4, 2024 15:10
Show Gist options
  • Save winebarrel/0c702fc80a9475855ee14fd5cdf14911 to your computer and use it in GitHub Desktop.
Save winebarrel/0c702fc80a9475855ee14fd5cdf14911 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import configparser
import json
import sys
def print_json(f):
config = configparser.ConfigParser()
config.read_file(f)
d = dict()
for sec in config.sections():
items = config.items(sec)
d[sec] = dict(items)
out = json.dumps(d)
print(out)
args = sys.argv
args.pop(0)
if len(args) == 0:
print_json(sys.stdin)
else:
for fn in args:
with open(fn) as f:
print_json(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment