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

CheddaShredda

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'))
Kamal Keswani

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'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to append and overwrite at the same time a file from an input in Python

From Dev

In IPython, how do you save and append to a file rather than overwriting it?

From Dev

python overwrite and append to text file

From Dev

Reading from a file rather than from the console

From Dev

Overwrite text file on first write, then append to it - Python

From Dev

Overwrite text file on first write, then append to it - Python

From Dev

How to append to a field in Capybara (Webdriver) rather than clear and populate

From Dev

How to Append a child to the outerHTML of the father element rather than inside?

From Dev

Lift 2.6: How to append a NodeSeq rather than using SetHtml()

From Dev

How to workwith generators from file for tokenization rather than materializing a list of strings?

From Dev

how to get argparse to read arguments from a file with an option rather than prefix

From Dev

Overwrite file in gulp stream

From Dev

How can I write to a file but keep \n as it is rather than adding a new line in python?

From Dev

How do I make Laravel Elixir merge my files rather than overwrite them?

From Dev

How do I make Laravel Elixir merge my files rather than overwrite them?

From Dev

How to download docx file rather than loading file in Firefox

From Dev

How to download docx file rather than loading file in Firefox

From Dev

How to show full results, rather than matched text from regex searches in python

From Dev

PHP access file from online download link rather than direct

From Dev

How to get ffmpeg to append to existing ouput file and not overwrite it?

From Dev

How to fork a process in Node.js to run JS code from a buffer available in parent rather than from a file?

From Dev

Robohelp: How to reference a .chm file rather than copy it into a merged project?

From Dev

How can I pass in text rather than a file as a variable

From Dev

How to record audio using naudio onto byte[] rather than file

From Dev

How to open a file with a double click rather than JFileChooser

From Dev

How can I pass in text rather than a file as a variable

From Dev

How do I APPEND or OVERWRITE from a variable in BASH?

From Dev

Selecting from Python lists using values rather than indices

From Dev

Run python script from Finder rather than edit?

Related Related

  1. 1

    How to append and overwrite at the same time a file from an input in Python

  2. 2

    In IPython, how do you save and append to a file rather than overwriting it?

  3. 3

    python overwrite and append to text file

  4. 4

    Reading from a file rather than from the console

  5. 5

    Overwrite text file on first write, then append to it - Python

  6. 6

    Overwrite text file on first write, then append to it - Python

  7. 7

    How to append to a field in Capybara (Webdriver) rather than clear and populate

  8. 8

    How to Append a child to the outerHTML of the father element rather than inside?

  9. 9

    Lift 2.6: How to append a NodeSeq rather than using SetHtml()

  10. 10

    How to workwith generators from file for tokenization rather than materializing a list of strings?

  11. 11

    how to get argparse to read arguments from a file with an option rather than prefix

  12. 12

    Overwrite file in gulp stream

  13. 13

    How can I write to a file but keep \n as it is rather than adding a new line in python?

  14. 14

    How do I make Laravel Elixir merge my files rather than overwrite them?

  15. 15

    How do I make Laravel Elixir merge my files rather than overwrite them?

  16. 16

    How to download docx file rather than loading file in Firefox

  17. 17

    How to download docx file rather than loading file in Firefox

  18. 18

    How to show full results, rather than matched text from regex searches in python

  19. 19

    PHP access file from online download link rather than direct

  20. 20

    How to get ffmpeg to append to existing ouput file and not overwrite it?

  21. 21

    How to fork a process in Node.js to run JS code from a buffer available in parent rather than from a file?

  22. 22

    Robohelp: How to reference a .chm file rather than copy it into a merged project?

  23. 23

    How can I pass in text rather than a file as a variable

  24. 24

    How to record audio using naudio onto byte[] rather than file

  25. 25

    How to open a file with a double click rather than JFileChooser

  26. 26

    How can I pass in text rather than a file as a variable

  27. 27

    How do I APPEND or OVERWRITE from a variable in BASH?

  28. 28

    Selecting from Python lists using values rather than indices

  29. 29

    Run python script from Finder rather than edit?

HotTag

Archive