Python Cycling binary file - remove from start add to end

Can someone point me to a method for cycling a binary in Python? I have a file full of 4 byte integers basically and when the file reaches a certain size, i.e. A certain number of values ​​have been recorded, I want to start deleting one from the beginning and add it to the end.

I'm still new to Python, so just trying to come up with a neat way to do this.

Thank.

+3
source share
2 answers

My idea: the first integer in the file gives you the position of the actual start of the data. At the beginning it will be 4 (assuming the integer takes 4 bytes). When the file is full, you simply start overwriting the data at the beginning and increase the integer. This is basically a simple ring buffer as a file.

+3
source

2000 numbers?

This is 16K. Do it in memory. Indeed, by declaring your 16K buffers, you can probably do the whole operation in a single I / O request. And on some large 64-bit systems, 2000 numbers larger or smaller is the default buffer size.

The amount of data is microscopic. Do not waste time optimizing such a small amount of data.

with open( "my file.dat", "rb", 16384 ) as the_file:
    my_circular_queue = list( read_the_numbers( the_file ) )

if len(my_circular_queue) >=  2000:
    my_circular_queue = my_circular_queue[1:]
my_circular_queue.append( a_new_number )

with open( "my file.dat", "wb", 16384 ) as the_file:
    write_the_numbers( the_file, my_circular_queue )

. , .

+3

Source: https://habr.com/ru/post/1770275/


All Articles