I have seen python code where we first open a file to read it, and then again open it and write to it. When I try to do both at the same time it gives an error. From the documentation, I don't see an 'rwb' option. It's only one of create/read/write/append mode.
So what I was trying to do was:
with gzip.GzipFile(stage3_filepath, 'rwb') as file:
data = json.loads(file.read().decode('utf-8'))
data["articles"].extend(article_list)
json_string = json.dumps(data, ensure_ascii=False)
file.write(json_string.encode('utf-8'))
Just wanted to know why it is not possible to do so? And is there any alternative option?
As asked by others, my motive here is to avoid extra time taken to open a file twice. It seems less for 1 to 2 files but for a large collection of files in billion, this can be a huge improvement.