Answer the question
In order to leave comments, you need to log in
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
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] ~>
import subprocess
proc = subprocess.run(["du","-s","/var/www/media"], stdout=subprocess.PIPE)
folder_size = int(proc.stdout.decode('utf-8').split('\t')[0])
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question