Python, Deleting all files in a folder older than X days

PythonPython 2.7

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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
			pass
  • 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.)

C:\scratch\removeThem\four.txt
C:\scratch\removeThem\one.txt
C:\scratch\removeThem\three.txt
C:\scratch\removeThem\two.txt

Solution 3 - Python

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:

Solution 4 - Python

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)):
            print(filename)
            os.remove(os.path.join(path, filename))

Solution 5 - Python

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:
     print(filename)

Solution 6 - Python

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

#!/usr/bin/python

# 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.

Solution 7 - Python

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")

Solution 8 - Python

With comprehensions, Can be:

import os
from time import time


p='.'
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]
print(result)
  • 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

Solution 9 - Python

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

try:
	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)):
					print(filename)
					os.remove(os.path.join(path, filename))
				elif os.path.isdir(os.path.join(path, filename)):
					print(filename)
					shutil.rmtree((os.path.join(path, filename)))
					os.remove(os.path.join(path, filename))
except:
	pass
				
print("Maintenance Complete!")

Solution 10 - Python

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

    def remove_files():
        removed=0
        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):
                  os.remove(curpath)
                  removed+=1
        print(removed)

Solution 11 - Python

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")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1681573View Question on Stackoverflow
Solution 1 - PythonkindallView Answer on Stackoverflow
Solution 2 - PythonBill BellView Answer on Stackoverflow
Solution 3 - PythonJoran BeasleyView Answer on Stackoverflow
Solution 4 - Pythonuser9652688View Answer on Stackoverflow
Solution 5 - PythonMansur Ul HasanView Answer on Stackoverflow
Solution 6 - PythonXman ClassicalView Answer on Stackoverflow
Solution 7 - PythonsantoshView Answer on Stackoverflow
Solution 8 - PythonbritodfbrView Answer on Stackoverflow
Solution 9 - Pythongabeyww0View Answer on Stackoverflow
Solution 10 - PythonCory RonquistView Answer on Stackoverflow
Solution 11 - Pythonmanu bharadwajView Answer on Stackoverflow