'Python is wrting huge files

Basically, I am receiving binary data from a server (Update rate is 4Hz) and I need to write this file in a binary file.

The problem in my code is the file size.

I am writing and the file get a huge size. I did the acquisition with a generic data logger software and the file size was less than 3mb for 5 minutes and with my code was more than 100mb.

To connect to the server I am using sockets (TCP) and write I am using the code below:

import socket

host = '10.1.1.10'
port = 4204
buffer_size = 2048

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            
s = socket.connect((host, port))
               
while True:

    data = s.recv(buffer_size)
    
    if not data:
        break
    
    data += data 
    
    with open('test2.000', 'a+b') as f:
        f.write(data)

Thank you in advance for the help.

Vinny



Solution 1:[1]

Your code seems to be appending the latest data received to all data received, and then writing all of it again. Removing

data += data 

should give you what you want.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aquaplane