码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-10-18 14:58:30      阅读:1470      评论:0      收藏:0      [点我收藏+]

标签:filetype   download   技术   安装   already   ext   for   aries   using   

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

 

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

标签:filetype   download   技术   安装   already   ext   for   aries   using   

原文地址:http://www.cnblogs.com/longsheng/p/7685868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!