2

I am giving a try in backend and I am failing in parsing Twitters stream API. I want to create Json file with timestamp, name, tweet and screen name. That seems to be working. But when I try to write it in file - entry overwrites already existing one. It does not continues. I red here that for some outfile.write('\n') worked. Tried and still new entry overwrites previous one

    with open('text2', 'w') as outfile:
        json.dump({'time': time.time(), 'screenName': screenName, 'text': text, 'name': name}, outfile, indent = 4, sort_keys=True)
        outfile.write('\n')
andris
  • 99
  • 4
  • 13
  • 1
    possible duplicate of [How do you append to file in python?](http://stackoverflow.com/questions/4706499/how-do-you-append-to-file-in-python) – RomanHotsiy Jul 16 '14 at 14:37
  • no, it's not. You can't append content to a json file like it would be a txt. – Alex M.M. Feb 24 '20 at 11:54

1 Answers1

2

When you open the file use a (append) instead of w (write).

https://docs.python.org/2/library/functions.html#open

Holloway
  • 6,412
  • 1
  • 26
  • 33