I have my json file that is:
{
"DIR": "/home/yabir/code",
"DISK": [
"/home/yabir/back",
"455G",
"8,6G",
"423G",
"2%",
"/home/yabir/back"
],
"FIRST": false,
"last": "13012015_222441",
"last_save": [
"/home/yabir/code/pygame/puzzle.py",
"/home/yabir/code/pygame/memorypuzzle.py",
"/home/yabir/code/pygame/Project",
"A/elementos.xcf",
"/home/yabir/code/clank/src/database.py",
"/home/yabir/code/clank/src/engine.py",
"/home/yabir/code/clank/src/pruebas.py",
"/home/yabir/code/clank/src/.git/hooks/applypatch-msg.sample",
"/home/yabir/code/clank/src/.git/hooks/post-update.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-applypatch.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-rebase.sample",
"/home/yabir/code/clank/src/.git/hooks/commit-msg.sample",
"/home/yabir/code/clank/src/.git/hooks/update.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-commit.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-push.sample",
"/home/yabir/code/clank/src/.git/hooks/prepare-commit-msg.sample",
"/home/yabir/code/clank/src/.git/HEAD",
"/home/yabir/code/clank/src/.git/info/exclude",
"/home/yabir/code/clank/src/.git/config",
"/home/yabir/code/clank/src/.git/description",
"/home/yabir/code/clank/src/xml2json.py",
"/home/yabir/code/clank/src/LICENSE",
"/home/yabir/code/clank/src/main.py",
"/home/yabir/code/clank/src/save.json",
"/home/yabir/code/clank/src/main.pyc",
"/home/yabir/code/clank/src/disks_manager.py",
"/home/yabir/code/clank/src/xml2json.pyc",
"/home/yabir/code/clank/src/structure.py"
]
}
but when I do
data["last_save"] = cmd_decoded
data["last"] = now
json_data.seek(0)
json_data.write(json.dumps(data,sort_keys=True,indent=4, separators=(',', ': ')))
where cmd_decoded is a list of strings like in the json file before "last_save". It replace correctly the "last" value but at the moment to modify "last_save" instead python just add at the end of the json file after the final }. Final resut:
{
"DIR": "/home/yabir/code",
"DISK": [
"/home/yabir/back",
"455G",
"8,6G",
"423G",
"2%",
"/home/yabir/back"
],
"FIRST": false,
"last": "13012015_222512",
"last_save": [
"/home/yabir/code/pygame/puzzle.py",
"/home/yabir/code/pygame/memorypuzzle.py",
"/home/yabir/code/pygame/Project",
"A/elementos.xcf",
"/home/yabir/code/clank/src/database.py",
"/home/yabir/code/clank/src/engine.py",
"/home/yabir/code/clank/src/pruebas.py",
"/home/yabir/code/clank/src/.git/hooks/applypatch-msg.sample",
"/home/yabir/code/clank/src/.git/hooks/post-update.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-applypatch.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-rebase.sample",
"/home/yabir/code/clank/src/.git/hooks/commit-msg.sample",
"/home/yabir/code/clank/src/.git/hooks/update.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-commit.sample",
"/home/yabir/code/clank/src/.git/hooks/pre-push.sample",
"/home/yabir/code/clank/src/.git/hooks/prepare-commit-msg.sample",
"/home/yabir/code/clank/src/.git/HEAD",
"/home/yabir/code/clank/src/.git/info/exclude",
"/home/yabir/code/clank/src/.git/config",
"/home/yabir/code/clank/src/.git/description",
"/home/yabir/code/clank/src/xml2json.py",
"/home/yabir/code/clank/src/save.json",
"/home/yabir/code/clank/src/disks_manager.py",
"/home/yabir/code/clank/src/xml2json.pyc"
]
}/code/clank/src/main.pyc",
"/home/yabir/code/clank/src/disks_manager.py",
"/home/yabir/code/clank/src/xml2json.pyc",
"/home/yabir/code/clank/src/structure.py"
]
}
wich should be in "last_save", and "last_save" still being like before running the script. Why the json file is not changing correctly?
At the begginig of my file I have:
json_data = open('save.json', "r+")
data = json.load(json_data)
And at the end:
json_data.close()