R
R
ReD2021-10-12 10:00:38
linux
ReD, 2021-10-12 10:00:38

How to implement the deletion of files before reaching a certain amount of free space?

Hello!

The task is to write a Python script that would delete files from a certain directory not completely, but before reaching a given threshold of free space.

What is the best way to implement this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Cheremisin, 2021-10-12
@trinitr0

It's very simple - read the output of the du command with the -s switch
. And only then decide what and how to delete.

[email protected] ~> du -s /var/www/media
131268256	/var/www/media
[email protected] ~>

Of course, you can go through all the subdirectories in python and count the sizes of all files. But why, if there is already a utility.
import subprocess
proc = subprocess.run(["du","-s","/var/www/media"], stdout=subprocess.PIPE)
folder_size = int(proc.stdout.decode('utf-8').split('\t')[0])

Well, you can delete it with the rm command

R
Ronald McDonald, 2021-10-12
@Zoominger

1. See how much free space is left;
2. If not enough, then delete the files from the directory;
3. Repeat.
You need the os module.
Everything.

S
Saboteur, 2021-10-12
@saboteur_kiev

Why python, when bash is just right for such tasks, or even find?
Why do you need to delete when you run out of space, and not initially adjust the rotation of files that grow - this is how it is done in normal cases

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question