Skip to content

Instantly share code, notes, and snippets.

@nicholastjohnson
Created October 10, 2014 15:33
Show Gist options
  • Save nicholastjohnson/5334bb3730eb90072a46 to your computer and use it in GitHub Desktop.
Save nicholastjohnson/5334bb3730eb90072a46 to your computer and use it in GitHub Desktop.
Python script to delete all files less than a certain size in a directory and subdirectories
import os, os.path
for root, _, files in os.walk("C:/some/dir"):
for f in files:
fullpath = os.path.join(root, f)
try:
if os.path.getsize(fullpath) < 10 * 1024: #set file size in kb
print fullpath
os.remove(fullpath)
except WindowsError:
print "Error" + fullpath
@Krisselack
Copy link

Nice little Script, thanks. For Python3 brackets at the print statetements would come in handy.

@rojobobo
Copy link

Thank you for posting this - very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment