Skip to content Skip to sidebar Skip to footer

Python - Delete Old Files

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 sear

Solution 1:

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 inos.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)

Solution 2:

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

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

flushdir(path)

Post a Comment for "Python - Delete Old Files"