I’m trying to write a python script to delete all files in a folder older than X days. This is what I have so far:

import os, time, sys
path = r"c:\users\%myusername%\downloads"
now = time.time()

for f in os.listdir(path):
  if os.stat(f).st_mtime < now - 7 * 86400:
    if os.path.isfile(f):
      os.remove(os.path.join(path, f))

When I run the script, I get:

Error2 - system cannot find the file specified,

and it gives the filename. What am I doing wrong?

os.listdir() returns a list of bare filenames. These do not have a full path, so you need to combine it with the path of the containing directory. You are doing this when you go to delete the file, but not when you stat the file (or when you do isfile() either).

Easiest solution is just to do it once at the top of your loop:

f = os.path.join(path, f)

Now f is the full path to the file and you just use f everywhere (change your remove() call to just use f too).

I think the new pathlib thingy together with the arrow module for dates make for neater code.

from pathlib import Path
import arrow

filesPath = r"C:\scratch\removeThem"

criticalTime = arrow.now().shift(hours=+5).shift(days=-7)

for item in Path(filesPath).glob('*'):
    if item.is_file():
        print (str(item.absolute()))
        itemTime = arrow.get(item.stat().st_mtime)
        if itemTime < criticalTime:
            #remove it
  • pathlib makes it easy to list the directory contents, to access file characteristics such as as creation times and to get full paths.
  • arrow makes calculations of times easier and neater.

Here’s the output showing the full paths offered by pathlib. (No need to join.)


You need to give it the path also or it will look in cwd.. which ironically enough you did on the os.remove but no where else…

for f in os.listdir(path):
    if os.stat(os.path.join(path,f)).st_mtime < now - 7 * 86400:

You need to use if os.stat(os.path.join(path, f)).st_mtime < now - 7 * 86400: instead of if os.stat(f).st_mtime < now - 7 * 86400:

I find using os.path.getmtime more convenient :-

import os, time

path = r"c:\users\%myusername%\downloads"
now = time.time()

for filename in os.listdir(path):
    # if os.stat(os.path.join(path, filename)).st_mtime < now - 7 * 86400:
    if os.path.getmtime(os.path.join(path, filename)) < now - 7 * 86400:
        if os.path.isfile(os.path.join(path, filename)):
            os.remove(os.path.join(path, filename))

i did it in more sufficient way

import os, time

path = "/home/mansoor/Documents/clients/AirFinder/vendors"
now = time.time()

for filename in os.listdir(path):
    filestamp = os.stat(os.path.join(path, filename)).st_mtime
    filecompare = now - 7 * 86400
    if  filestamp < filecompare:

A simple python script to remove /logs/ files older than 10 days


# run by crontab
# removes any files in /logs/ older than 10 days

import os, sys, time
from subprocess import call

def get_file_directory(file):
    return os.path.dirname(os.path.abspath(file))

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

files = os.listdir(os.path.join(get_file_directory(__file__), "logs"))
file_path = os.path.join(get_file_directory(__file__), "logs/")
for xfile in files:
    if os.path.isfile(str(file_path) + xfile):
        t = os.stat(str(file_path) + xfile)
        c = t.st_ctime

        # delete file if older than 10 days
        if c < cutoff:
            os.remove(str(file_path) + xfile)

With __file__ you can replace by your path.

This deletes files older than 60 days.

import os

directory = '/home/coffee/Documents'

os.system("find " + directory + " -mtime +60 -print")
os.system("find " + directory + " -mtime +60 -delete")

With comprehensions, Can be:

import os
from time import time

result=[os.remove(file) for file in (os.path.join(path, file) for path, _, files in os.walk(p) for file in files) if os.stat(file).st_mtime < time() - 7 * 86400]
  • remove files with match = os.remove(file)
  • loop for all files into path = for file in
  • generation with all files = (os.path.join(path, file) for path, _,
    files in os.walk(p) for file in files)
  • p is a directory into filesystem
  • verify mtime to match= if os.stat(file).st_mtime < time() – 7 * 86400

May be see: https://ideone.com/Bryj1l

Here’s how I do it on my Windows machines. It uses shutil to also remove subdirectories created in downloads. I also have a similar one to keep the folders cleaned up on the hard drive of my son’s computer, as he has special needs and tends to let things get out of control fast.

import os, time, shutil

paths = (("C:"+os.getenv('HOMEPATH')+"\Downloads"), (os.getenv('TEMP')))
oneday = (time.time())- 1 * 86400

    for path in paths:
        for filename in os.listdir(path):
            if os.path.getmtime(os.path.join(path, filename)) < oneday:
                if os.path.isfile(os.path.join(path, filename)):
                    os.remove(os.path.join(path, filename))
                elif os.path.isdir(os.path.join(path, filename)):
                    shutil.rmtree((os.path.join(path, filename)))
                    os.remove(os.path.join(path, filename))
print("Maintenance Complete!")

would like to add what i came up with to do this task.
the function is called in the login process.

    def remove_files():
        path = "desired path"
        # Check current working directory.
        dir_to_search = os.getcwd()
        print "Current working directory %s" % dir_to_search
        #compare current to desired directory
        if dir_to_search != "full desired path":
            # Now change the directory
            os.chdir( desired path )
            # Check current working directory.
            dir_to_search = os.getcwd()
            print "Directory changed successfully %s" % dir_to_search
        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=1):

Some of the other answers also have the same code but i feel they have overcomplicated a very simple process

import os
import time

#folder to clear from
dir_path="path of directory to clean"
#No of days before which the files are to be deleted
limit_days = 10

treshold = time.time() - limit_days*86400
entries = os.listdir(dir_path)
for dir in entries:
    creation_time = os.stat(os.path.join(dir_path,dir)).st_ctime
    if creation_time < treshold:
        print(f"{dir} is created on {time.ctime(creation_time)} and will be deleted")