Dec-27-2017, 07:32 AM
Please, unless it is really big project, post your code in code tags, don't attach py files. See BBcode help for more info. This will make easier to answer and will attract more answers.
I added these for your this time.
Your problems comes every time when you use the same file name you append the json object to the file. As a result what you get is not A valid json file. e.g. after two 'runs' your json file will look like this
there are other problems with your code
1. you overwrite dname immediately
2. It is better to implement __str__() method instead of print_json()
see https://docs.python.org/3/reference/data...tomization
3. unless you write to file, before you print, you will not have _filename property set and this will rise an error. i.e. if you try to print the json, before you write it to file.
I added these for your this time.
Your problems comes every time when you use the same file name you append the json object to the file. As a result what you get is not A valid json file. e.g. after two 'runs' your json file will look like this
Output:{"bar": "moo"}{"bar": "moo"}
you need to rethink your logic - e.g. load the json file and then append the new key-value pairs and then overwrite the existing file.there are other problems with your code
1. you overwrite dname immediately
dname=str(input('select dictionary name: ')) dname= D()first it is a string, a "name" and immediately you use same variable for D instance, so you lost the name
2. It is better to implement __str__() method instead of print_json()
see https://docs.python.org/3/reference/data...tomization
3. unless you write to file, before you print, you will not have _filename property set and this will rise an error. i.e. if you try to print the json, before you write it to file.