Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

cleanup.py

Tags: removed
#-*-coding:utf8;-*-
#qpy:3
#qpy:console

import os
import math


def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
units = int(math.floor(math.log(size_bytes, 1024)))
power = math.pow(1024, units)
size = round(size_bytes / power, 2)
return "%s %s" % (size, size_name[units])

def crawl(start_dir):
for root, _, files in os.walk(start_dir):
for filename in files:
yield os.path.join(root, filename)

def clean_dir(directory):
removed = 0
for filepath in crawl(directory):
removed += os.stat(filepath).st_size
print('removing {}'.format(filepath))
os.remove(filepath)
print('\nremoved {}'.format(convert_size(removed)))

clean_dir('sdcard/Download/')



This post first appeared on Ricky's Python Notes, please read the originial post: here

Share the post

cleanup.py

×

Subscribe to Ricky's Python Notes

Get updates delivered right to your inbox!

Thank you for your subscription

×