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())
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:
(Not thoroughly tested, I recommend a test run substituting “os.remove” with “print” ;))