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 7734471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:15:29+00:00 2026-06-01T07:15:29+00:00

An application that writes data to disk in 100MB chunks and increments the file-name

  • 0

An application that writes data to disk in 100MB chunks and increments the file-name by appending +1, so n1, n2 ... n1000. This eventually uses all of the free space on the partition (Linux host). I am looking for a way to delete files that were first written in the series until the drive space is under a specific utilization.

If the latter, would https://stackoverflow.com/a/5912404/666891 be a good solution?

The following solution was proposed and looks to be a viable solution per https://stackoverflow.com/a/837840/666891. How could this be modified to handle the incrementing file extension as currently when the script is run it does not delete files name filename*, asterisk being the incrementing number, start with the oldest one?

import os
def free_space_up_to(free_bytes_required="161061273600", rootfolder="/data/", ex
tension="filename-*"):
    file_list= files_to_delete(rootfolder, extension)
    while file_list:
        statv= os.statvfs(rootfolder)
        if statv.f_bfree*statv.f_bsize >= free_bytes_required:
            break
        os.remove(file_list.pop())
  • 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-06-01T07:15:30+00:00Added an answer on June 1, 2026 at 7:15 am

    Well, if you know that all files are (at least sort of) 100MB in size, and assuming there’s nothing else drastically altering disk usage on the machine, you don’t need to check for free space at every iteration.

    Also, if all files have the same name, besides the counter at the end, you can skip the os.stat call (which could also be useless for files created in quick succession) and sort on the filenames based on the counter:

    import os
    
    def free_space_up_to(free_bytes_required=161061273600, rootfolder="/data/", filesize=104857600, basename="filename-"):
        '''Deletes rootfolder/basename*, oldest first, until there are free_bytes_required available on the partition.
        Assumes that all files have file_size, and are all named basename{0,1,2,3,...}
        Returns number of deleted files.
        '''
        statv = os.statvfs(rootfolder)
        required_space = free_bytes_required - statv.f_bfree*statv.f_bsize
        basepath = os.path.join(rootfolder, basename)
        baselen = len(basepath)
        if required_space <= 0:
            return 0
    
        # "1 +" here for quickly rounding
        files_to_delete = 1 + required_space/filesize
    
        # List all matching files. If needed, replace with os.walk for recursively
        # searching into subdirectories of rootfolder
        file_list = [os.path.join(rootfolder, f) for f in os.listdir(rootfolder)
                     if f.startswith(basename)]
    
        file_list.sort(key=lambda i: int(i[baselen:]), reverse=True)
        # Alternatively, if the filenames can't be trusted, sort based on modification time
        #file_list.sort(key=lambda i: os.stat(i).st_mtime)
    
        for f in file_list[:files_to_delete]:
            os.remove(f)
        return files_to_delete
    

    (Not thoroughly tested, I recommend a test run substituting “os.remove” with “print” ;))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that receives chunks of data over the network, and writes
I have a .NET winform application that writes an XML data file, but I
I have a UNIX application written in ansi C that writes data directly to
I am developing a application that needs to store data with many writes and
I have a server application that writes to a popen(myCommand, w) file descriptor in
I have created an application that writes some data to the root folder of
My application writes some bytes of data to an alternate data stream. This works
I have an application that write data in the programdata folder in vista. The
I need to write an application that migrates data from a RavnDB to SQL.
I'm trying to write a javascript application that loads data from the openstreetmap API

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.