【Python】图片有效性管理
2021-03-01 04:28
标签:== imp name wal print home cep pytho lse 【Python】图片有效性管理 标签:== imp name wal print home cep pytho lse 原文地址:https://www.cnblogs.com/defineconst/p/14451397.html# -*- coding: UTF-8 -*-
import os
import imghdr
def get_filelist(dir):
for home, dirs, files in os.walk(dir):
for filename in files:
try:
fullname = os.path.join(home, filename)
filesize = os.path.getsize(fullname)
# print(fullname + ",文件大小为 "+ str(filesize))
if filesize :
print(fullname + ",错误文件大小为 "+ str(filesize))
try:
os.remove(fullname)
print("删除成功" + fullname)
except:
print("删除失败" + fullname)
elif filesize :
if imghdr.what(fullname):
# good image
print(fullname + " is OK")
else:
# bad image
print(fullname + ",无效文件大小为 "+ str(filesize))
try:
os.remove(fullname)
print("删除成功" + fullname)
except:
print("删除失败" + fullname)
except:
print("属性获取失败," + fullname)
if __name__ == "__main__":
get_filelist("E:\\15")