Quantcast
Viewing all articles
Browse latest Browse all 22

Python script to remove /tmp/ files older than 7 days

For no reason at all, I needed a simple trimmer program. Rather than write a shell script in bash, thought to do it in python.

#!/usr/bin/python

# run by crontab
# removes any files in /tmp/ older than 7 days

import os, sys, time
from subprocess import call

now = time.time()
cutoff = now - (7 * 86400)

files = os.listdir("/tmp")
for file in files:
        if os.path.isfile( "/tmp/" + file ):
                t = os.stat( "/tmp/" + file )
                c = t.st_ctime

                # delete file if older than a week
                if c < cutoff:
                        os.remove("/tmp/" + file)

Save this as /wherever/trim_tmp.py, and chmod +x it for good measure. Then, add to crontab with crontab -e. In the localized user crontab file, put @daily /wherever/trim_tmp.py

The post Python script to remove /tmp/ files older than 7 days appeared first on Dino's Anabasis.


Viewing all articles
Browse latest Browse all 22

Trending Articles