Search code examples
pythonfilestreammessage

How to append to a file from stream rather than overwrite in Python


I am new to python so this maybe a simple question but I have a kafka consumer from which I read in messages. Every time a new message comes in it rewrites the previous message into the order.json file however I want to append it instead. Additionally, I want to make sure the messages do not get read in no faster than every 1 second using possible some sort of pause. Any tips on how to do this would be much appreciated. Here is my current code

for message in consumer:
     with open('order.json', 'w') as file:
          file.write(message.value.decode('UTF-8'))

Solution

  • Open the file in append mode as 'w' (write mode) truncates the file each time it exists

    for message in consumer:
        with open('order.json', 'a') as file:
            file.write(message.value.decode('UTF-8'))