python批量修改文件名的实现代码
admin
2023-07-31 02:09:42
0
#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)

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...