Last active
August 29, 2015 13:56
-
-
Save anderssonfilip/8955633 to your computer and use it in GitHub Desktop.
Simple Python script to cap disk space of a folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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