欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python批量修改文件名的實現代碼

 更新時間:2014年09月01日 16:09:48   投稿:mdxy-dxy  
這篇文章主要介紹了python批量修改文件名的實現代碼,需要的朋友可以參考下
#coding:utf-8 
#批量修改文件名 
import os import re import datetime 
 
re_st = r'(\d+)\+\s?\((\d+)\)'
 #用于匹配舊的文件名,需含分組 re_match_old_file_name = re.compile(re_st) 
 #要修改的目錄 WORKING_PATH = r'F:\Gallery'
 
 #---------------------------------------------------------------------- 
def rename_fomat(name): 
  """ 
  文件重命名格式組織模塊(一般修改這里就可以了) 
  NOTE:返回類型必須是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 
 #---------------------------------------------------------------------- 
def logs(error): 
  """ 
  錯誤記錄 
  """
  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'寫入日志失敗'
  finally: 
    LOG_FILE.close() 
 #---------------------------------------------------------------------- 
def rename(old, new): 
  """ 
  文件重命名模塊 
  return: 
    0:rename success 
    1:the new path is exists 
    -1:rename failed 
  """
  if not os.path.exists(new): 
    try: 
      os.renames(old, new) 
      return 0
    except IOError: 
      print 'path error:', new 
      return -1
  else: 
    return 1
 #---------------------------------------------------------------------- 
def get_dirs(path): 
  """ 
  獲取目錄列表 
  """
  if os.path.exists(path): 
    return os.listdir(path) 
  else: 
    return -1
 
 #---------------------------------------------------------------------- 
def get_input_result(word, choice): 
  """ 
  獲取正確的輸入結果 
  """
  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 
   
 
 #---------------------------------------------------------------------- 
def batch_rename(index, dirs = []): 
  """ 
  批量修改文件 
  """
  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): 
  """ 
  測試 
  返回過濾結果 
  """
  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 
   #---------------------------------------------------------------------- 
def manage(index): 
  """ 
  程序組織塊 
  """
  file_filter = batch_rename_test(index) 
  do_choice = get_input_result( 
    word = 'Do with this(y / n)', 
    choice = ['y', 'n'] 
  ) 
  if do_choice == 'y': 
    batch_rename(index, dirs= file_filter) 
  print 'Finished !'
 
 if __name__ == '__main__': 
  path = WORKING_PATH 
  manage(index = path)

相關文章

  • keras訓練曲線,混淆矩陣,CNN層輸出可視化實例

    keras訓練曲線,混淆矩陣,CNN層輸出可視化實例

    這篇文章主要介紹了keras訓練曲線,混淆矩陣,CNN層輸出可視化實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 利用Python進行圖像的加法,圖像混合(附代碼)

    利用Python進行圖像的加法,圖像混合(附代碼)

    這篇文章主要介紹了利用Python進行圖像的加法,圖像混合(附代碼),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • Python爬蟲進階之爬取某視頻并下載的實現

    Python爬蟲進階之爬取某視頻并下載的實現

    這篇文章主要介紹了Python爬蟲進階之爬取某視頻并下載的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • python3中bytes和string之間的互相轉換

    python3中bytes和string之間的互相轉換

    這篇文章主要介紹了python3中bytes和string之間的互相轉換,文中給出了詳細的介紹和示例代碼,相信對大家具有一定的參考價值,有需要的朋友們下面來一起學習學習吧。
    2017-02-02
  • PyTorch實現AlexNet示例

    PyTorch實現AlexNet示例

    今天小編就為大家分享一篇PyTorch實現AlexNet示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python學習實操案例(四)

    python學習實操案例(四)

    這篇文章主要介紹了python學習實操案例,這一篇小編給大家?guī)淼氖橇斜?,所以這里是和列表有關的案例,需要的小伙伴可以參考一下,希望對你有所幫助<BR>
    2022-02-02
  • python自動生成model文件過程詳解

    python自動生成model文件過程詳解

    這篇文章主要介紹了python自動生成model文件過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值
    2019-11-11
  • 利用Python搶回在螞蟻森林逝去的能量(實現代碼)

    利用Python搶回在螞蟻森林逝去的能量(實現代碼)

    螞蟻森林是一項旨在帶動公眾低碳減排的公益項目,每個人的低碳行為在螞蟻森林里可計為"綠色能量",很多小伙伴都玩過,今天小編給大家分享一篇教程關于Python搶回在螞蟻森林逝去的能量,感興趣的朋友跟隨小編一起看看吧
    2022-03-03
  • 對python3 一組數值的歸一化處理方法詳解

    對python3 一組數值的歸一化處理方法詳解

    今天小編就為大家分享一篇對python3 一組數值的歸一化處理方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python使用multiprocessing創(chuàng)建進程的方法

    Python使用multiprocessing創(chuàng)建進程的方法

    這篇文章主要介紹了Python使用multiprocessing創(chuàng)建進程的方法,實例分析了multiprocessing模塊操作進程的相關技巧,需要的朋友可以參考下
    2015-06-06

最新評論