Python3 use the filetype package to find all corrupted files in the specified directory (Pictures, videos, audio, etc.)

Source: Internet
Author: User

There is a problem with the stored hard disk, most of the picture files are OK after the system reboot, but there is a little damage. Python makes it easy to find all the corrupted files from a few terabytes of files.

Make sure you are using a Python version 2.7.9 or above or more than 3.4

PIP is already installed if you ' re using Python 2 >=2.7.9 or Python 3 >=3.4 binaries downloaded fr Om python.org, but you'll need to upgrade PIP.
PIP is installed by default in Python version 2.7.9 or above or 3.4 python

1) Install filetype extensions using commands
Pip Install filetype

2) Use the following code to export the list of corrupted files

Just change the path you want to look for in the directory you want to find.

#-Coding:utf-8-ImportIMGHDRImportOSImportdatetimeImport TimeImportCodecsImportfiletypecheckstart_date= Datetime.datetime.now () + datetime.timedelta (days =-77)#Check how many days the file is withinCheckstart_str = Checkstart_date.strftime ("%y-%m-%d") file= Codecs.open ('D:\imglist.txt','W','Utf-8')#write the absolute path of the damaged file to TXT file & prevent garbledPath ='D:\IMG\\'                                  #directories to check forPrint('Start checking') forRT, dirs, filesinchOs.walk (path): forFinchFiles:fname=Os.path.splitext (f) Statinfo=os.stat (rt+'\\'+f)ifTime.strftime ("%y-%m-%d", Time.localtime (statinfo.st_mtime)) >=Checkstart_str:kind= Filetype.guess (rt+'\\'+f)ifKind isNone:file.write (Rt+'\\'+f+'\ r \ n')                Print(rt+'\\'+f)Print('End Checking') File.close ()

Python3 use the filetype package to find all corrupted files in the specified directory (Pictures, videos, audio, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.