I'm writing a Python backup script and I need to find the oldest file in a directory (and its sub-directories). I also need to filter it down to *.avi files only.
The script will always be running on a Linux machine. Is there some way to do it in Python or would running some shell commands be better?
At the moment I'm running df
to get the free space on a particular partition, and if there is less than 5 gigabytes free, I want to start deleting the oldest *.avi
files until that condition is met.
To do it in Python, you can use
os.walk(path)
to iterate recursively over the files, and thest_size
andst_mtime
attributes ofos.stat(filename)
to get the file sizes and modification times.