python批量修改文件名代码_python批量修改文件名的实现代码

python批量修改文件名代码_python批量修改文件名的实现代码coding utf 8 批量修改文件名 importosimpo st r d s d 用于匹配旧的文件名 需含分组 re match old file name re compile re st 要修改的目录 WORKING PATH r F Gallery

#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)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233082.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 给js对象添加属性和方法属性_js给json对象添加属性

    给js对象添加属性和方法属性_js给json对象添加属性方式一:在定义对象时,直接添加属性和方法functionPerson(name,age){ this.name=name; this.age=age; this.say=function(){ alert(name+’:::’+age); }}varperson=newPerson(‘张三’,24);person.say();方式二:…

    2025年8月3日
    2
  • 在手写数字识别的例子中_关于人脸识别的案例

    在手写数字识别的例子中_关于人脸识别的案例1.概念介绍:图像识别(ImageRecognition)是指利用计算机对图像进行处理、分析和理解,以识别各种不同模式的目标和对像的技术。 图像识别的发展经历了三个阶段:文字识别、数字图像处理与识别、物体识别。机器学习领域一般将此类识别问题转化为分类问题。手写识别是常见的图像识别任务。计算机通过手写体图片来识别出图片中的字,与印刷字体不同的是,不同人的手写体风…

    2025年11月16日
    4
  • 通过PropertyDescriptor反射进行字段名值的获取及设置

    通过PropertyDescriptor反射进行字段名值的获取及设置/** *根据属性名获取对应的value *@paramfieldName *@paramobj *@return *@throwsException */privatestaticStringgetValueByFiled(StringfieldName,Objectobj)throwsException{  //属性扫描器

    2022年10月1日
    4
  • 1077. 皇宫看守(树形dp)[通俗易懂]

    1077. 皇宫看守(树形dp)[通俗易懂]太平王世子事件后,陆小凤成了皇上特聘的御前一品侍卫。皇宫各个宫殿的分布,呈一棵树的形状,宫殿可视为树中结点,两个宫殿之间如果存在道路直接相连,则该道路视为树中的一条边。已知,在一个宫殿镇守的守卫不仅能够观察到本宫殿的状况,还能观察到与该宫殿直接存在道路相连的其他宫殿的状况。大内保卫森严,三步一岗,五步一哨,每个宫殿都要有人全天候看守,在不同的宫殿安排看守所需的费用不同。可是陆小凤手上的经费不足,无论如何也没法在每个宫殿都安置留守侍卫。帮助陆小凤布置侍卫,在看守全部宫殿的前提下,使得花费的经费最少。

    2022年8月9日
    9
  • git diff提示filemode发生改变(old mode 100644、new mode 10075)[通俗易懂]

    git diff提示filemode发生改变(old mode 100644、new mode 10075)

    2022年2月12日
    43
  • mac怎么上传文件到服务器_window上传文件到linux

    mac怎么上传文件到服务器_window上传文件到linux前言我们使用mac时,想让本地文件上传至服务器,该怎么办呢windows系统,我们可以使用xftp或者rz命令,那么mac呢?mac系统,我们可以使用sftp、scp或者rz命令,本文介绍sft

    2022年7月30日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号