PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 12, 2022

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

 July 12, 2022     file, message, python, stream     No comments   

Issue

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


Answered By - Kamal Keswani
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing