0

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()
2
  • show the code where you open the file, also an example of what has been saved, it's not clear what you mean Commented Jan 13, 2015 at 21:38
  • @Anentropic Edited, just tell me if you need anything more Commented Jan 13, 2015 at 21:46

1 Answer 1

2

Your script opens a file, reads from it, then seeks back to its beginning and writes stuff to it, replacing parts of its contents as it goes. However, if what you're writing is smaller than the original contents of the file, this will only overwrite the same amount of data that you're writing.

It looks like you want to entirely replace the contents of the file when writing to it. You should open it in "r" for initial parsing, then close it and reopen it in "w" to write data to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.