Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6015645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:51:56+00:00 2026-05-23T02:51:56+00:00

I need a python script implementing a circular buffer for rows in a text

  • 0

I need a python script implementing a circular buffer for rows in a text file limited to N rows like this:

        row 1 -> pop
        row 2
        row 3
         |
         |
push -> row N

What’s the best solution?

EDIT:
This script should create and maintain the text file which only contains the latest N lines. Then it should pop the first line pushed in. Like a fifo buffer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:51:57+00:00Added an answer on May 23, 2026 at 2:51 am

    Try my recipe and sorry for italian usage:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    #       fifo(.py)
    #       
    #       Copyright 2011 Fabio Di Bernardini <fdb@altraqua.com>
    #       
    #       This program is free software; you can redistribute it and/or modify
    #       it under the terms of the GNU General Public License as published by
    #       the Free Software Foundation; either version 2 of the License, or
    #       (at your option) any later version.
    #       
    #       This program is distributed in the hope that it will be useful,
    #       but WITHOUT ANY WARRANTY; without even the implied warranty of
    #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    #       GNU General Public License for more details.
    #       
    #       You should have received a copy of the GNU General Public License
    #       along with this program; if not, write to the Free Software
    #       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    #       MA 02110-1301, USA.
    
    def string_conditioned(string):
        return string.decode('string_escape').rstrip() + '\n'
    
    def pop(n, size, filename):
        with open(filename, 'r+U') as fd:
            rows = fd.readlines()
        with open(filename, 'w') as fd:
            n = int(n)
            fd.writelines(rows[n:])
            return ''.join(rows[:n])
    
    def trim_fifo(row, size, filename):
        size = int(size)
        with open(filename, 'rU') as fd:
            rows = fd.readlines()
        num_rows = len(rows)
        if num_rows >= size:
            n = string_conditioned(row).count('\n')
            pop(num_rows + n - size, size, filename)
    
    def push(row, size, filename):
        trim_fifo(row, size, filename)
        with open(filename, 'a') as fd:
            fd.write(string_conditioned(row))
        return ''
    
    def main():
        import sys
        try:
            command  = sys.argv[1]
            param    = sys.argv[2]
            size     = sys.argv[3]
            filename = sys.argv[4]
            sys.stdout.write({
            '--push': push,
            '--pop' : pop,
            }[command](param, size, filename))
        except Exception, e:
            print r"""
    Uso:
           fifo --push ROW MAX_ROWS FILE
           fifo --pop  NUM MAX_ROWS FILE
    
    fifo implementa un buffer ad anello di righe di testo, Quando viene inserita
    una riga che fa superare il numero massimo di righe (MAX_ROWS) elimina la riga
    più vecchia.
    
    Comandi:
      --push    accoda la riga di testo ROW nel FILE rimuovendo le righe più vecchie
                se il file supera MAX_ROWS. Usare '\n' per separare righe multiple.
      --pop     stampa le prime NUM righe e le rimuove dal FILE. MAX_ROWS viene
                ignorato ma deve essere comunque specificato.
    
    Esempi:
           fifo --push 'row_one \n row_two' 10 fifo.txt
           fifo --pop 2 10 fifo.txt
    """
            print e
    
    if __name__ == '__main__':
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Python backup script and I need to find the oldest file
A python script I need to run takes input only from a file passed
I need to execute this python script: http://superjared.com/static/code/googleMX.py I installed pyactiveresource, but when I
I need to run a shell command asynchronously from a Python script. By this
I need a method to run a python script file, and if the script
I need to execute code from my python script and take interpreter-style output like
I need a python script, which will get selected text in an other application
Please I need help getting the exact md5 value of this python script in
Sometimes (in customer's PCs) I need a python script to execute in the Windows
A python script need to spawn multiple sub-processes via fork(). All of those child

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.