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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数组求和方法汇总_用函数的方法对输入的数组求和

    数组求和方法汇总_用函数的方法对输入的数组求和vararr=[1,2,3,4,5,6];测试时我不想过度使用全局变量影响命名空间,所以没使用未声明变量。而是直接通过私有作用域设置静态私有变量,也可以用其他设计模式来限定变量作用域。因为数组对象的迭代方法也是一种遍历,所以也可以借助用来实现求和。一、利用数组对象的各迭代方法:1.array.every()查询是否有所有项都匹配的方法:1(function(){…

    2022年9月28日
    3
  • datax(11):源码解读 ContainerCommunicator

    datax(11):源码解读 ContainerCommunicator前面看了datax的通讯类communication,现在看看在他之上包装的一个容器通信类ContainerCommunicator一、抽象基类AbstractContainerCommunicatordataX中提供了一个基类 AbstractContainerCommunicator来处理JobContainer、TaskGroupContainer和Task的通讯。AbstractContainerCommunicator提供了注册、收集信息等接口,信息的单位是Communication.

    2022年5月13日
    52
  • a标签去下划线或文字添加下修饰_a标签下划线(如何去掉a标签下划线)

    a标签去下划线或文字添加下修饰_a标签下划线(如何去掉a标签下划线)去掉a标签下划线:对超链接下划线设置使用代码"text-decoration"语法:text-decoration:none||underline||blink||overline||line-throughtext-decoration参数:none:.为什么我这个没有下划线?a:hover{color:red;}我爱你正常状态下的a标签是这…

    2022年5月2日
    74
  • java中线程安全的容器_jfinal容器线程安全吗

    java中线程安全的容器_jfinal容器线程安全吗四、线程安全的容器类Java编码中,我们经常需要用到容器来编程。在并发环境下,Java提供一些已有容器能够支持并发。1.Map在Map类中,提供两种线程安全容器。java.util.HashtableHashtable和HashMap类似,都是散列表,存储键值对映射。主要区别在于Hashtable是线程安全的。当我们查看Hashtable源码的时候,可以看到Hashtable的方法都是通过syn

    2022年9月12日
    1
  • IPV6服务器搭建「建议收藏」

    IPV6服务器搭建「建议收藏」环境准备:1.安装虚拟机(virtualbox)2.在虚拟机中安装Ubuntu系统3.dhcp-4.2.5.tar源码 配置虚拟机环境:1.配置虚拟机gcc命令,快捷键:CTRL

    2022年8月2日
    6
  • c++与三菱plc通信_C语言编写的代码是

    c++与三菱plc通信_C语言编写的代码是FX系列作为三菱基本款的PLC,它们之间的通讯体例别离如下:CC-LINK,N:N网络连接,并联连接。1.CC-LINK连接CC-LINK连接图如下:三菱PLC通讯与编程实例!-1.jpg(21.71KB,下载次数:0)2018-6-103:01上传对应的PLC可为FX1N、FX1NC、FX2N、FX2NC、FX3U、FX3UC,因为在使用CC-LINK通讯时要扩展CC-LINK模块,…

    2022年9月10日
    2

发表回复

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

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