Skip to content

Instantly share code, notes, and snippets.

@colehocking
Created February 27, 2020 01:50
Show Gist options
  • Save colehocking/ca0763708ca894acf90f92856c3a343e to your computer and use it in GitHub Desktop.
Save colehocking/ca0763708ca894acf90f92856c3a343e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Calculate sum of files older than X date
# File is large csv w/ format: filename,date time:time,x.xxx(mb)
# -- Cole Hocking
import csv
from datetime import datetime, timedelta
from sys import argv, exit
import os.path as opath
def validateFile(filei):
"""
Check for file_exists
"""
if opath.isfile(filei):
return True
else:
print("%r not found" % (filei))
exit(1)
def getFileData(file_name):
"""
Calculates Files >= 7 years and their total size
"""
#timedelta does not take years arg; arg=7*365
seven_year_mkr = datetime.today() - timedelta(days=2555)
#print(seven_year_mkr)
#size of the files
size_total = 0.000
#number of files
file_sum = 0
line_no = 0
with open(file_name, "r") as csvFile:
csv_reader = csv.reader(csvFile, delimiter='|')
for row in csv_reader:
line_no += 1
#print(line_no)
last_accessed = datetime.strptime(row[1], '%m/%d/%Y %H:%M:%S')
#greater than means younger than the marker
if last_accessed > seven_year_mkr:
continue
#conditions here for files OLDER||= seven_year_mkr
else:
# strips commas for float normalization
size_total += float(row[2].replace(',',''))
file_sum += 1
print("# of files >= 7 years: %d" % file_sum)
print("Size of files >= 7 years: %r mb" % size_total)
def main(argv):
if len(argv) != 2:
print("Usage: ./file_sum.py <file-to-parse>")
exit(1)
assert(validateFile(argv[1]))
getFileData(argv[1])
if __name__ == "__main__":
main(argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment