user3447653 user3447653 - 3 months ago 8
Python Question

Append new lines to the existing file in Pandas dataframe

I have a python script that runs a query and stores it in a dataframe as below:

df = pd.read_gbq(queryString, project_id=PROJECT)

out = df.to_json(orient='records')[1:-1].replace('},{', '}\n{')


After some processing, I current store it in a json file:

with open('/home/user/Downloads/Count1/Count.json', 'w') as f:
f.write(out)


Every time when the program runs, it overwrites the existing file. I was just wondering whether there is a method in Python that appends only the new lines to the existing file rather than overwriting the entire file.

Any help would be appreciated !!

Answer

Try this:

with open('/home/user/Downloads/Count1/Count.json', 'ab') as f:
     f.write(out)