Python batch modification of file name implementation code _python

Source: Internet
Author: User
#coding: utf-8 #批量修改文件名 import OS import re import datetime Re_st = R ' (\d+) \+\s?\ ((\d+) \) ' #用于匹配旧的文件名, including grouping Re_match_ Old_file_name = Re.compile (re_st) #要修改的目录 Working_path = R ' F:\Gallery ' #--------------------------------------------- -------------------------def rename_fomat (name): "" "File Rename format organization module (general modification here is OK) Note: The return type must be Unicode" "" If NA Me: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.%0 4d '% (num) return NEW_NM #----------------------------------------------------------------------def logs (Erro  R): "" "" "" "" "" "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 = error[' error_paths '] for item in Errors:item = '%s\n '% item LOG + = Item Log = Log.encode (' utf-8 ') try:LOG_FILE.write (log) except Ioerror:print u ' write log failed ' finally 
  : Log_file.close () #----------------------------------------------------------------------def rename (old, new): The file Rename module Return:0:rename success 1:the New path is Exists-1:rename failed "" If not OS.P  
      Ath.exists (New): Try:os.renames (old, new) return 0 except Ioerror:print ' path error: ', new Return-1 Else:return 1 #----------------------------------------------------------------------def get_ Dirs (path): "" Get directory List "" If Os.path.exists (path): Return Os.listdir (path) else:return-1 #-- --------------------------------------------------------------------def get_input_result (Word, choice): "" To get the right loss Into the result "" "Correct_result = set (choice) word = ' ===%s?\n[in]: '% (word) while true:in_choice = Raw_input (wor D) if In_choice inCorrect_result:return In_choice #----------------------------------------------------------------------def BATC H_rename (index, dirs = []): "" Batch Modify File "" "index = Unicode (index) errors = [] if dirs = = []: dirs = Get_dirs (Path = index) if dirs and dirs!= []: for item in Dirs:item = Unicode (item) new_name = Ren Ame_fomat (item) If new_name:old_pt = U '%s\\%s '% (index, item) new_pt = U '%s\\%s '% (index, new_n 
        AME) 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 (di 
        RS): 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 ER Rors!= []: 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 #------------------------------------------------------------------- ---def manage (index):
  "" "Program Organization block" "File_filter = Batch_rename_test (index) Do_choice = get_input_result (Word = ' do with" (y/n) ', choice = [' Y ', ' n ']] if do_choice = = ' Y ': batch_rename (Index, dirs= file_filter) print ' Fini
 
 Shed! ' if __name__ = = ' __main__ ': path = Working_path Manage (index = path)

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.