#!/usr/bin/env python#-*-coding:utf-8-*-" "A Python script that modifies the name of a photo file in bulk. Traverse the photo file of the specified directory (including subdirectories) and modify the photo's file name to the following format according to the time Taken: 20140315_091230.jpg (%y%m%d_%h%m%s) The probability of the name being named is very small because the filename is already accurate to the second. If you need to consider the problem of duplicate name, this program can be further optimized. The program needs to install the Exifread module, otherwise it cannot be used. For example, Linux/mac OS x installs the module under command line: sudo pip install exifread" "ImportOSImportStatImport TimeImportExifreadmy_date_format='%y%m%d_%h%m%s'Suffix_filter= ['. jpg','. PNG','. mpg','. mp4','. tHM','. bmp','. JPEG','. avi','. mov']delete_files= ['Thumbs.db','Sample.dat' ]defisformatedfilename (filename):#determine if the file name is already formatted Try: Filename_nopath=os.path. basename (filename) F, e=Os.path. Splitext (Filename_nopath) time Strptime (f, My_date_format)returnTrueexceptValueError:returnFalsedefistargetedfiletype (filename):#determine if the file type you want to process is based on the file name extensionFilename_nopath =os.path. basename (filename) F, e=Os.path. Splitext (Filename_nopath)ifE. Lower ()inchSuffix_filter:returnTrueElse : returnFalsedefisdeletefile (filename):#determine if the file you want to delete is specifiedFilename_nopath =os.path. basename (filename)ifFilename_nopath. Lower ()inchDelete_files:returnTrueElse : returnFalsedefgeneratenewfilename (filename):#generate a new file name based on the photo's time taken (if you don't get the photo time, the file creation time is used) Try : ifos.path. Isfile (filename): FD= open (filename,'RB' ) Else : Raise "[%s] is not a file!\n"%filenameexcept : Raise "Unopen file[%s]\n"%filename Data=exifread. Process_file (FD)ifData:#get the date of the photo taken Try: T= data ['EXIF datetimeoriginal' ] #Convert to YYYYMMDD_HHMMSS formatDatestr = str (t). Replace (":","") [: 10] +"_"+ str (t) [11:]. Replace (":","" ) except : Pass #If no EXIF information is obtained, the date of creation of the image file is taken asState =OS. stat (filename) datestr= time. Strftime (My_date_format, Time localtime (state [-2])) dirname=os.path. dirname (filename) Filename_nopath=os.path. basename (filename) F, e=Os.path. Splitext (filename_nopath) NewFileName= Os.path. Join (dirname, Datestr +e). Lower ()returnNewFileNamedefScandir (startdir):#traverse the specified directory and subdirectories to rename or delete files that meet the criteriaos. ChDir (Startdir) forObjinchos. Listdir (OS. CurDir):ifos.path. Isfile (obj):ifIstargetedfiletype (obj) andIsformatedfilename (obj) = =False:#renaming files that meet filtering criteriaNewFileName =generatenewfilename (obj)Print "Rename [%s] = [%s]"%(obj, newfilename) os. Rename (obj, newfilename)elifisdeletefile (obj):#Delete a developed file Print "Delete [%s]:"%obj os. Remove (obj)Else : Pass ifos.path. Isdir (obj): scandir (obj) os. chdir (OS. Pardir)if __name__=="__main__": Path="/users/abc/www/img"scandir (path)
[Classic] Use Python to batch rename the photos taken by the iphone-follow the shooting time to rename