问题描述
将数据以 100MB 块写入磁盘并通过附加 +1 来增加文件名的应用程序,因此 n1, n2 ... n1000
.这最终会使用分区(Linux 主机)上的所有可用空间.我正在寻找一种方法来删除系列中首次写入的文件,直到驱动器空间达到特定利用率.
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.
如果是后者,https://stackoverflow.com/a/5912404/666891 会是一个好的解决方案吗?
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())
推荐答案
好吧,如果您知道所有文件的大小(至少大约是 100MB),并且假设没有其他事情会彻底改变机器上的磁盘使用情况,那么您不需要在每次迭代时检查可用空间.
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.
此外,如果所有文件都具有相同的名称,除了末尾的计数器,您可以跳过 os.stat 调用(这对于快速连续创建的文件也可能无用)并根据计数器对文件名进行排序:
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
(未经过彻底测试,我建议进行测试运行,将os.remove"替换为print";))
(Not thoroughly tested, I recommend a test run substituting "os.remove" with "print" ;))
这篇关于全盘删除最旧的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!