A
A
AlbertBorman2022-03-22 16:42:21
Python
AlbertBorman, 2022-03-22 16:42:21

Error when rewriting json via python?

I wrote a BASH script, I needed a base, I decided to make it on json, overwrite it using python, and then parse it using JQ, I hadn’t done a base on json before, as a result, after a lot of trial and error, everything finally worked, then I added the keys (also for the first time) and everything stopped working

#!/usr/bin/env python

import argparse 
import json

parser = argparse.ArgumentParser(description="test")
parser.add_argument("-i", dest="id", required=True)
parser.add_argument("-a", dest="api", required=True)
args = parser.parse_args()

def a(id, api):
    with open("tg.json", "rt", encoding="utf-8") as file:
        settings = json.load(file)
 
    settings["id"] = {id}
    settings["api"] = {api}

    with open("tg.json", "wt", encoding="utf-8") as file:
        json.dump(settings, file, indent=4)
print ({id})
rc, message = a(args.id, args.api)

The error lies in the fact that everything in the json file hung at the moment the "block" id was written, the rest of the database simply disappeared.

PS: the code is not mine at all, everything is copied from the Internet (do not scold)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question