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)
上一篇 2025年8月19日 上午9:01
下一篇 2025年8月19日 上午9:22


相关推荐

  • 线与逻辑

    线与逻辑线与逻辑 OC 门 又称集电极开路 漏极开路 与非门门电路 OpenCollecto OpenDrain 为什么引入 OC 门 实际使用中 有时需要两个或两个以上与非门的输出端连接在同一条导线上 将这些与非门上的数据 状态电平 用同一条导线输送出去 因此 需要一种新的与非门电路 OC 门来实现 线与逻辑 OC 门主要用于 3 个方面 实现与或非逻辑 用做电平转换 用做驱动器 由于 OC 门电路的输出管

    2026年3月20日
    2
  • 关于sql和MySQL的语句执行顺序(必看!!!)[通俗易懂]

    关于sql和MySQL的语句执行顺序(必看!!!)[通俗易懂]今天遇到一个问题就是mysql中insertinto和update以及delete语句中能使用as别名吗?目前还在查看,但是在查阅资料时发现了一些有益的知识,给大家分享一下,就是关于sql以及MySQL语句执行顺序:sql和mysql执行顺序,发现内部机制是一样的。最大区别是在别名的引用上。一、sql执行顺序(1)from(3)join(2)on(4)where…

    2022年6月4日
    31
  • js 加载初始化日期

    js 加载初始化日期

    2022年1月3日
    51
  • win10查看激活时间

    win10查看激活时间win10查看激活时间1、使用Windows+R组合快捷键打开运行命令框运行:slmgr.vbs-dlv命令可以查询到Win10的激活信息,包括:激活ID、安装ID、激活截止日期等信息。看不懂的继续往下。2、运行:slmgr.vbs-dli命令可以查询到操作系统版本、部分产品密钥、许可证状态等。3、运行:slmgr.vbs-xpr命令可以查询Win10是否永久激活…

    2022年5月30日
    52
  • 冯骥评价Seedance 2.0:AI理解多模态信息并整合的能力完成了一次飞跃

    冯骥评价Seedance 2.0:AI理解多模态信息并整合的能力完成了一次飞跃

    2026年3月13日
    3
  • Car Fleet 车队

    Car Fleet 车队N  辆车沿着一条车道驶向位于 target 英里之外的共同目的地。每辆车 i 以恒定的速度 speed[i] (英里/小时),从初始位置 position[i] (英里)沿车道驶向目的地。一辆车永远不会超过前面的另一辆车,但它可以追上去,并与前车以相同的速度紧接着行驶。此时,我们会忽略这两辆车之间的距离,也就是说,它们被假定处于相同的位置。车队 是一些由行驶在相同位置、具有相同…

    2022年5月15日
    76

发表回复

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

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