summaryrefslogtreecommitdiff
path: root/Chap9PracDelUnneed.py
blob: f277c0d930f2b7f3ef6630d4aea3810ce38bbb95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python3

# Chapter 9 Practice Selective Copy
# Walks through a directory tree searching for files > 100MB and deletes them.

import os

def deleteUnneeded(folder):
    folder = os.path.abspath(folder)
    for foldername, subfolders, filenames in os.walk(folder):
        for filename in filenames:
            fileSize = os.path.getsize(foldername + '/' + filename)
            if int(fileSize) < 10000000:
                continue
            #os.unlink(filename) #Commented out to protect against accidental deletion
            print('Deleting ' + filename + '...') #Print only to verify working correctly

deleteUnneeded('/Users/username/Documents')
print('Done')