Python batch file name modification implementation code,
# Coding: UTF-8 # batch Modify file names import OS import re import datetime re_st = R' (\ d +) \ + \ s? \ (\ D +) \) '# used to match the old file name, which must contain the re_match_old_file_name = re. compile (re_st) # WORKING_PATH = r 'f: \ Gallery '# ---------------------------------------------------------------------------- def rename_fomat (name ): "file rename format organization module (this can be changed here) NOTE: The returned type must be unicode" if name: re_rn = re_match_old_file_name.findall (name) if re_rn and re_rn! = []: Re_rn = re_rn [0] num = int (re_rn) new_nm = u'no. % 04d '% (num) return new_nm # javasdef logs (error): "error log" "log = ''log_file = open (R '. /log.txt ', 'A') live_info = "========== Time: % s title: % s Path: % s ============ "% (datetime. datetime. now (), str (error ['title']), str (error ['index']),) log + = live_info errors = e Rror ['error _ paths '] for item in errors: item =' % s \ n' % item log + = item log = log. encode ('utf-8') try: LOG_FILE.write (log) handle T IOError: print U' write log failed 'Finally: LOG_FILE.close () # define def rename (old, new ): "file rename module return: 0: rename success 1: the new path is exists-1: rename failed" if not OS. path. exists (new): try: OS. renam Es (old, new) return 0 distinct T IOError: print 'path error: ', new return-1 else: return 1 # define def get_dirs (path ): "Get directory list" if OS. path. exists (path): return OS. listdir (path) else: return-1 # javasdef get_input_result (word, choice): "Get correct input result" correct_result = Set (choice) word = '===% s? \ N [in]: '% (word) while True: in_choice = raw_input (word) if in_choice in correct_result: return in_choice # define def batch_rename (index, dirs = []): "batch file modification" index = unicode (index) errors = [] if dirs = []: dirs = get_dirs (path = index) if dirs and dirs! = []: For item in dirs: item = unicode (item) new_name = rename_fomat (item) if new_name: old_pt = u '% s \ % s' % (index, item) new_pt = u '% s \ % s' % (index, new_name) res_rn = rename (old_pt, new_pt) if res_rn! = 0: errors. append (item) else: errors. append (item) if errors and errors! = []: Print 'rename Failed: 'logs ({'index': index, 'title': 'rename failed', 'error _ paths ': errors ,}) for I, item in enumerate (errors): print item, '|', if I % 5 = 4: print ''print ''else: return-1 # ---------------------------------------------------------------------- def batch_rename_test (index): "test return filter result" index = unicode (index) errors = [] correct = [] dirs = get_dirs (path = index) If dirs and dirs! = []: For x, item in enumerate (dirs): item = unicode (item) new_name = rename_fomat (item) if new_name: correct. append (item) old_pt = u '% s \ % s' % (index, item) new_pt = u' % s \ % s' % (index, new_name) print '[% d] O: % s' % (x + 1, old_pt) print' [% d] N: % s' % (x + 1, new_pt) else: errors. append (item) if errors and errors! = []: Print 'not Match: 'logs ({'index': index, 'title': 'Not Match', 'error _ paths ': errors ,}) for I, item in enumerate (errors): print item, '|', if I % 5 = 4: print ''print ''return correct # define def manage (index): "program organization block" file_filter = batch_rename_test (index) do_choice = get_input_result (word = 'do with this (y/n) ', ch Oice = ['y', 'n']) if do_choice = 'y': batch_rename (index, dirs = file_filter) print 'finished! 'If _ name _ = '_ main _': path = WORKING_PATH manage (index = path)