Hi, I am working on a requirement where I download packages from a repository using Python. To improvise the script further, we want to have a setup where we store the versions of the libraries/packages we have downloaded, and do not repeat the process if the packages are already present. Additionally, if there is a newer version available, only then go ahead with the download. I am using a json file to have information regarding the packages that are downloaded.
I am facing an issue with regards to updating/appending the existing json file:
{
"libraries":[ {
"name": "package1",
"version": "12.0"
}, {
"name": "package2",
"version": "12.0"
}
]
}
Any suggestions on my error and how can I achieve this?
I am facing an issue with regards to updating/appending the existing json file:
import json import os def writefunc(name_value, location_value): with open("my_json.json", 'a+') as f: ##creating a new file during the start if os.stat("my_json.json").st_size == 0: data = { "libraries": { "name": name_value, "version": location_value } } json.dump(data, f, indent=4) else: dump = json.loads(f.read()) ## errror here as I cannot read the data on the file print(dump) if __name__ == "__main__": writefunc('package1', '11.0') print('added package1') writefunc('package2', '12.0') print('added package2') writefunc('package1', '12.0') print('added package1')Expected output :
{
"libraries":[ {
"name": "package1",
"version": "12.0"
}, {
"name": "package2",
"version": "12.0"
}
]
}
Any suggestions on my error and how can I achieve this?