import argparse
import os
import time

__author__ = 'Anthony'

# Sets up command line argument parsing
parser = argparse.ArgumentParser(description="Deletes files older than specified age in specified directory.")
parser.add_argument('directory', help='The directory to delete old files from')
parser.add_argument('age', type=int, help='Files this age or older will be deleted (in days)')
args = parser.parse_args()

file_dir = args.directory # directory to search for old files in
file_age = args.age * 60 * 60 * 24 # puts age in seconds

for root, dirs, files in os.walk(file_dir): # Loops through all files in the given directory
    for name in files:
        file_path = os.path.join(root,name) # Gets the full file path
        mod_time = os.stat(file_path).st_mtime # Gets last modified time
        if (time.time() - file_age > mod_time): # The file is older than the set age
            os.remove(file_path)