Delete files older than N days in Python
Last Updated :
04 Dec, 2022
In this article, we will learn to delete files that are older than a given number of days using Python. This work can be done manually but it is difficult to do when files are more in number, so we will use Python to make this task simple. We will learn three ways to do this task using Python:
- Delete files using OS and time modules
- Delete files using os.walk and DateTime
- Delete files using shutil.rmtree of Shutil Module.
Deleting files using os and time modules
File Structure of directory where code exists.
File Structure of demo directory (this folder will be used for testing code) before running code.
Code Implementation:
Importing the necessary modules and Defining the folder in which the action must be done. The N stands for the number of days. The folder where we need to do the delete operation should be changed to the current working directory. Obtain a list of every file that exists in the specified directory hence Check whether or not each file is older than N days by looping through all of them. Delete any file that is more than N number of days old.
Python3
import os
import time
folder = "demo"
N = 3
os.chdir(os.path.join(os.getcwd(), folder))
list_of_files = os.listdir()
current_time = time.time()
day = 86400
for i in list_of_files:
file_location = os.path.join(os.getcwd(), i)
file_time = os.stat(file_location).st_mtime
if (file_time < current_time - day * N):
print (f " Delete : {i}" )
os.remove(file_location)
|
Output:
File Structure of demo directory after running code.
File Structure of directory where code exists:
File Structure of demo directory (this folder will be used for testing code) before running code.
Code Implementation:
Import the required modules. Define the folder in which we have to perform the operation. Define the value of N which is the number of days. Define a function to perform the delete operation based on the condition. Loop using os.walk to check all files one by one. Get the modification time of each file using datetime module. If any file is older than N days then delete it.
Python3
import os
import datetime
folder = "demo"
N = 3
def check_and_delete(folder):
for (root,dirs,files) in os.walk(folder, topdown = True ):
for f in files:
file_path = os.path.join(root,f)
timestamp_of_file_modified = os.path.getmtime(file_path)
modification_date = datetime.datetime.fromtimestamp(timestamp_of_file_modified)
number_of_days = (datetime.datetime.now() - modification_date).days
if number_of_days > N:
os.remove(file_path)
print (f " Delete : {f}" )
check_and_delete(folder)
|
Output:
File Structure of demo directory after running code.
Deleting files using shutil.rmtree
File Structure of directory where code exists:
File Structure of demo directory (this folder will be used for testing code) before running code.
Code Implementation:
Import required modules and define the folder in which we have to perform the operation. Define the value of N which is the number of days. Change the current working directory to the folder in which we have to perform the delete operation. Get a list of all files present in the given directory. Loop over all the files and check whether they are older than N days or not. If any file is older than N days then delete it using os.remove(). If any folder is older than N days then delete it using shutil.rmtree().
Python3
import os
import time
import shutil
folder = "demo"
N = 3
os.chdir(os.path.join(os.getcwd(), folder))
list_of_files = os.listdir()
current_time = time.time()
day = 86400
for i in list_of_files:
file_location = os.path.join
(os.getcwd(), i)
file_time = os.stat
(file_location).st_mtime
if (file_time < current_time - day * N):
print (f " Delete : {i}" )
if os.path.isfile(file_location):
os.remove(file_location)
elif os.path.isdir(file_location):
shutil.rmtree(file_location)
|
Output:
File Structure of demo directory after running code.
Share your thoughts in the comments
Please Login to comment...