I wanted to add loop recording feature for the Raspberry Pi camera system project (link to earlier project). This will free up the space in the thumb drive which enable 24 x 7 x 365 continuous recording.
Basically I added a few lines of code into the existing script, the code will delete any files that are more than 7 days old. The script as below, you will need to import datetime and os libraries.
I learn the script from this cool website Python How To , which teaches people how to code to solve common problems. Please check out the website, and the link to the script : https://pythonhowtoprogram.com/how-to-remove-files-older-than-7-days-using-python-script/
path = '/mnt/mydisk/'
today = datetime.datetime.today()#gets current time
os.chdir(path) #changing path to current path(same as cd command)
#Iterate thru files
for root, directories, files in os.walk(path,topdown=False):
for name in files:
#last modified time
timeStamp = os.stat(os.path.join(root, name))[8]
filetime = datetime.datetime.fromtimestamp(timeStamp) - today
#check if recirdings file is more than 7 days old, if yes then delete them
if filetime.days <= -7:
print(os.path.join(root, name), filetime.days)
os.remove(os.path.join(root, name))