Delete files older than N days in Python
Nowadays, we create many files of many types and at the end up creating many useful as well as unnecessary files in our computer system. However, it will not create many difficulties for normal users, it is very crucial for developers to delete such old files in order to save storage and time as otherwise, users have to delete them manually.
In Python, delete files older than N days
In this tutorial, we will learn to delete a file that is older more than 90 days.
For that, we require 3 libraries,
import os import sys import datetime
Next, we will enter the file path and pass it to the function(req_path).
req_path=input("Enter your path: ") # we will enter file path and pass it to req_path del_older_files(req_path) # pass value to the function old_older_files
Now, after that, we will implement our main function(del_older_files) in which we used our mechanism which is described below,
def del_older_files(req_path): N=90 # Enter number of old file you want to delete if not os.path.exists(req_path):# if input path not exist then it will show below error print("Please provide valid path") sys.exit(1) if os.path.isfile(req_path):# if the given file path is file then it will show below error print("Please provide dictionary path") sys.exit(2) today=datetime.datetime.now()# it will get currunt datetime and transfer to variable(today) for each_file in os.listdir(req_path): each_file_path=os.path.join(req_path,each_file) if os.path.isfile(each_file_path): file_cre_date=datetime.datetime.fromtimestamp(os.path.getctime(each_file_path))# Here, fortimestamp will get the date of file created dif_days=(today-file_cre_date).days if dif_days > N:# if the age(N) of file is older then 90 days then conditions becomes True and remove that file os.remove(each_file_path) print(each_file_path,dif_days)
Here, N is the age of the file like here it is 90 years old. Then in the 3rd line, it will check whether the file path exists or not then if it exists then in the 4th step it will check is it a file or folder if it is a folder then it will show an error.
Moreover, through datetime object, we fetch today’s date and further, we have used the fromtimestamp function to get date of the file and subtract days to get the final output which means we will delete older files that are 90 days old.
Final code:
import os import sys import datetime def del_older_files(req_path): N=90 if not os.path.exists(req_path): print("Please provide valid path") sys.exit(1) if os.path.isfile(req_path): print("Please provide dictionary path") sys.exit(2) today=datetime.datetime.now() for each_file in os.listdir(req_path): each_file_path=os.path.join(req_path,each_file) if os.path.isfile(each_file_path): file_cre_date=datetime.datetime.fromtimestamp(os.path.getctime(each_file_path)) dif_days=(today-file_cre_date).days if dif_days > N: os.remove(each_file_path) print(each_file_path,dif_days) req_path=input("Enter your path: ") del_older_files(req_path)
Leave a Reply