Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - delete old files

Tags:

python

I'm somewhat new to python and have been trying to figure this out on my own but only getting bits and pieces so far. Basically i'm looking for a script that will recursively search a directory and it's sub-directories and delete files that are at least 24 hours old but not alter the directories. Any advice or examples are greatly appreciated.

like image 807
user915852 Avatar asked Aug 27 '11 20:08

user915852


2 Answers

This uses the os.walk method to recursively search a directory. For each file, it checks the modified date with os.path.getmtime and compares that with datetime.now (the current time). datetime.timedelta is constructed to create a timedelta of 24 hours.

It searches the directory os.path.curdir which is the current directory when the script is invoked. You can set dir_to_search to something else, e.g. a parameter to the script.

import os
import datetime

dir_to_search = os.path.curdir
for dirpath, dirnames, filenames in os.walk(dir_to_search):
   for file in filenames:
      curpath = os.path.join(dirpath, file)
      file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath))
      if datetime.datetime.now() - file_modified > datetime.timedelta(hours=24):
          os.remove(curpath)
like image 100
jterrace Avatar answered Nov 16 '22 09:11

jterrace


If you need it to check all files in all directories recursively, something like this ought to do:

import os, time

path = "/path/to/folder"
def flushdir(dir):
    now = time.time()
    for f in os.listdir(dir):
        fullpath = os.path.join(dir, f)
        if os.stat(fullpath).st_mtime < (now - 86400):
            if os.path.isfile(fullpath):
                os.remove(fullpath)
            elif os.path.isdir(fullpath):
                flushdir(fullpath)

flushdir(path)
like image 21
Gabriel Ross Avatar answered Nov 16 '22 07:11

Gabriel Ross