Skip to content

Instantly share code, notes, and snippets.

@Anton-Cao
Last active December 24, 2018 07:13
Show Gist options
  • Save Anton-Cao/d6ddfdeefa759f598f049ea3ba5d1088 to your computer and use it in GitHub Desktop.
Save Anton-Cao/d6ddfdeefa759f598f049ea3ba5d1088 to your computer and use it in GitHub Desktop.
Python script that copies new key-value pairs from sample_config.json to config.json
'''
Python script that copies new key-value pairs from sample_config.json to config.json
'''
import json
sample_file = open('sample_config.json', 'r')
sample_contents = json.loads(sample_file.read())
sample_file.close()
try:
config_file = open('config.json', 'r')
config_contents = json.loads(config_file.read())
config_file.close()
except: # file doesn't exist or is empty
config_contents = {}
for key, val in sample_contents.items():
if key not in config_contents:
config_contents[key] = val
with open('config.json', 'w') as config_file:
json.dump(config_contents, config_file, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment