Skip to content

Instantly share code, notes, and snippets.

@anderssonfilip
Last active August 29, 2015 13:56
Show Gist options
  • Save anderssonfilip/8955633 to your computer and use it in GitHub Desktop.
Save anderssonfilip/8955633 to your computer and use it in GitHub Desktop.
Simple Python script to cap disk space of a folder
import os
cap = 1 # cap in Megabytes
from datetime import date, timedelta
from time import gmtime
cutoffDate = date.today()-timedelta(days=1) # does not delete files newer than this (exclusive of the date)
def GetFolderSize(start_path = '.'): #get disk space in Megabytes
return sum(os.path.getsize(f) for f in os.listdir('.') if os.path.isfile(f)) / (1024*1024)
if GetFolderSize() > cap:
for file in os.listdir('.'):
if os.path.isfile(file) and f != os.path.basename(__file__):
if gmtime(os.path.getmtime(file)) <= cutoffDate.timetuple():
os.remove(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment