Last active
October 13, 2017 13:24
-
-
Save 007bsd/57637c04679bc99f3250a5cc0d4d9ed5 to your computer and use it in GitHub Desktop.
Modifying a json content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import os | |
| class Programs: | |
| def __init__(self): # class constructur (called at creation time) | |
| self.code = "" # the default name is the empty string | |
| def catalogcode(code): | |
| script_dir = os.path.dirname(__file__) # <-- absolute dir the script is in | |
| rel_path = "20171013063854.json" | |
| abs_file_path = os.path.join(script_dir, rel_path) | |
| with open(abs_file_path, "r+") as jsonFile: | |
| data = json.load(jsonFile) | |
| print (data['meta']['catalog_version']) | |
| data['meta']['catalog_version'] = code # <--- add `id` value. | |
| jsonFile.seek(0) # <--- should reset file position to the beginning. | |
| json.dump(data, jsonFile, indent=4) | |
| jsonFile.truncate() | |
| print (data['meta']['catalog_version']) | |
| return catalogcode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment