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


相关推荐

  • Apap图像配准算法[通俗易懂]

    Apap图像配准算法[通俗易懂]Apap图像配准算法

    2022年4月19日
    47
  • navicate15最新激活码_在线激活

    (navicate15最新激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html1STL5S9V8F-eyJsa…

    2022年3月27日
    102
  • android 浏览器控件[通俗易懂]

    android 浏览器控件[通俗易懂]在Android手机中内置了一款高性能webkit内核浏览器,在SDK中封装为一个叫做WebView组件。 什么是webkit WebKit是MacOSXv10.3及以上版本所包含的软件框架(对v10.2.7及以上版本也可通过软件更新获取)。同时,WebKit也是MacOSX的Safari网页浏览器的基础。WebKit是一个开源项目,主要由KDE的KHTML修改而来并且

    2022年5月15日
    40
  • 优化算法——模拟退火算法

    优化算法——模拟退火算法模拟退火算法原理模拟退火算法模拟退火算法过程模拟退火算法流程模拟退火算法的Java实现Java代码最后的结果模拟退火算法原理爬山法是一种贪婪的方法,对于一个优化问题,其大致图像(图像地址)如下图所示:其目标是要找到函数的最大值,若初始化时,初始点的位置在CC处,则会寻找到附近的局部最大值AA点处,由于AA点出是一个局部最大值点,故对于爬山法来讲,该算法无法跳出局部最大值点。若初始

    2022年7月18日
    18
  • 如何用 python gzip解压?[通俗易懂]

    如何用 python gzip解压?[通俗易懂]#createadecompressgzipfilefunctionimportgzipimportosdefun_gzip(gzip_file):f_name=gzip_file.split(‘.’)[0]withgzip.open(gzip_file,’rb’)asf_in:withopen(f_name,’wb’)asf_out:f_out.writelines(f_in)

    2025年9月25日
    4
  • php实现笛卡尔积

    php实现笛卡尔积问题:有那么一个数组$arr=array(array(1,2,3),array(‘中’,’国’,’人’),array(‘a’,’b’,’c’));要求使用PHP语言实现计算该数组的笛卡尔积。实现过程如下所示:<?php$arr=array(array(1,2,3),array(‘…

    2022年7月27日
    6

发表回复

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

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