首页 > 编程 > Python > 正文

Python 遍历目录 按类型大小过滤文件 删除空目录

2019-11-10 20:02:35
字体:
来源:转载
供稿:网友
#coding:utf-8import osimage_types = [".jpg",".jpeg",".bmp",".png"]for parent,dirs,files in os.walk("D:/grab") :    for file in files :        file=os.path.join(parent,file)        size=os.path.getsize(file)        if size < 150 * 1024:            os.remove(file)            continue        type=os.path.splitext(file)[1]        if not type in image_types:            os.remove(file)for parent,dirs,files in os.walk("D:/grab") :    if len(files)==0 and len(dirs)==0:        os.removedirs(parent)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表