#!/usr/bin/env python3
import argparse
import fnmatch
import os
import sys
from datetime import datetime, timedelta
from pathlib import Path
# set date now.
now = datetime.today()
# setup dir to clean
home = str(Path.home())
target_dir = '/home/jasonr' # CHANGE TO WHERE YOU WANT TO SEARCH
# dir to clean
dirs_to_clean = target_dir
# setup cli arguments.
parser = argparse.ArgumentParser(
description='''
[--days_ago 60] will keep 60 days worth of files.
[--debug yes] will print out statements with no actions.''',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--days_ago',
help='[--days_ago NN]')
parser.add_argument('--debug',
help='[--debug (yes|no)')
args = parser.parse_args()
# allowed arguments from cli.
accepted_cli_args = ['yes', 'no']
# sanity check, assign days to keep on system.
if args.days_ago is None:
days = 60
else:
days = args.days_ago
# define a list of patterns
patterns = ['*.csv', '*.txt'] # YOU CAN ADD ANY PATTERN TO LIST
# sanity check, assign debug true or false
if args.debug in accepted_cli_args:
if args.debug == 'yes':
debug = True
else:
debug = False
else:
print("{0}: Wrong parameter --debug (yes or no): [{1}]"
.format(now, args.debug))
sys.exit(1)
def find_files(dir_to_clean):
file_list = []
days_ago = datetime.now() - timedelta(days=int(days))
for root, dirs, files in os.walk(dir_to_clean):
for pattern in patterns:
for filename in fnmatch.filter(files, pattern):
file_list.append(os.path.join(root, filename))
file_list.sort()
for file in file_list:
try:
file_atime = datetime.fromtimestamp(os.path.getatime(file))
except Exception as e:
print("{0}: File Access Time Get Failed: [{1}]"
.format(now, e))
if file_atime < days_ago:
if os.path.isfile(file):
try:
if not debug:
print("{0}: Removing file: [{1}]"
.format(now, file))
os.remove(file)
else:
print("{0}: DEBUG: Removing file: [{1}]"
.format(now, file))
except OSError as e:
print("{0}: File Clean Up Failed: [{1}]"
.format(now, e))
sys.exit(1)
# main function.
def main():
find_files(dirs_to_clean)
if __name__ == "__main__":
main()