Python script to loop through all files in directory, delete any that are less than 200 kB in size

This does directory and all subdirectories:

import os, os.path

for root, _, files in os.walk(dirtocheck):
    for f in files:
        fullpath = os.path.join(root, f)
        if os.path.getsize(fullpath) < 200 * 1024:
            os.remove(fullpath)

Or:

import os, os.path

fileiter = (os.path.join(root, f)
    for root, _, files in os.walk(dirtocheck)
    for f in files)
smallfileiter = (f for f in fileiter if os.path.getsize(f) < 200 * 1024)
for small in smallfileiter:
    os.remove(small)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)