Python3 使用filetype包查找指定目录中所有损坏的文件(图片,视频,音频等文件),,存储的硬盘出现了问题


存储的硬盘出现了问题,系统重启后大部分图片文件都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 imghdrimport osimport datetimeimport timeimport codecsimport filetypecheckstart_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()

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

评论关闭