Python3 使用filetype包查找指定目录中所有损坏的文件(图片,视频,音频等文件)

时间:2017-10-18 14:58:30   收藏:0   阅读:1470

存储的硬盘出现了问题,系统重启后大部分图片文件都OK,不过有少部分损坏。使用Python可以轻而易举的从几个TB的文件中找到所有损坏的文件。

确保所使用的Python版本2.7.9以上或者3.4以上

pip is already installed if you‘re using Python 2 >=2.7.9 or Python 3 >=3.4 binaries downloaded from python.org, but you‘ll need to upgrade pip.
pip默认安装在Python版本2.7.9以上或者3.4以上的Python

1)使用命令安装filetype扩展

pip install filetype

技术分享

2)使用以下代码导出损坏文件列表

只需把要查查找的路径path修改为你所需要查找的目录即可

#- coding:utf-8 -
import imghdr
import os
import datetime
import time
import codecs
import filetype

checkstart_date = datetime.datetime.now() + datetime.timedelta(days = -77) #检查多少天以内的文件
checkstart_Str = checkstart_date.strftime("%Y-%m-%d")
file = codecs.open(d:\imglist.txt, w,utf-8)  #把受损文件绝对路径写入txt文件&防止乱码
path = D:\IMG\\                                  #需要检查的目录 

print(start checking)
for rt, dirs, files in os.walk(path):
    for f in files:
        fname = os.path.splitext(f)
        statinfo=os.stat(rt+\\+f)
        if time.strftime("%Y-%m-%d",time.localtime(statinfo.st_mtime)) >= checkstart_Str:
            kind = filetype.guess(rt+\\+f)
            if kind is None:
                file.write(rt+\\+f+\r\n)
                print(rt+\\+f)                    

            
print(end checking)
file.close()

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!