Skip to content

Instantly share code, notes, and snippets.

@needcaffeine
Created December 10, 2023 23:04
Show Gist options
  • Save needcaffeine/3577a83ef45d88d8eeffdbe93886aaed to your computer and use it in GitHub Desktop.
Save needcaffeine/3577a83ef45d88d8eeffdbe93886aaed to your computer and use it in GitHub Desktop.
# given a bunch of folders which are years, and contents which are files of the form YYYY-mm-dd - name - $1234.56,
# this script returns a sum of totals by year
import os
totals = {}
for year in os.listdir('.'):
if os.path.isdir(year) and not year.startswith('.'):
total = 0
for file in os.listdir(year):
if file[0] != '.' and file != 'Reimbursed':
parts = file.split(' - ')
if len(parts) > 2:
amount = parts[2].rsplit('.', 1)[0] # Split from the right to keep decimal part
amount = amount.replace('$', '') # Remove dollar sign
total += float(amount)
totals[year] = total
# Sort the years and print the totals
for year in sorted(totals):
print(f'{year} {totals[year]:.2f}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment