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

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版