#coding:utf-8 #批量修改文件名 import os import re import datetime re_st = r'(\d+)\+\s""" 文件重命名格式组织模块(一般修改这里就可以了) 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""" 批量修改文件 """ 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)
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- ABC唱片-鲍比达·新民乐《满江红》[APE+CUE]
- 许嵩.2014-不如吃茶去【海蝶】【WAV+CUE】
- 周笔畅.2024-HAVE.A.GOOD.NIGHT【SN.Music】【FLAC分轨】
- 周笔畅.2024-HAVE.A.NICE.DAY【SN.Music】【FLAC分轨】
- 证声音乐图书馆《真夏派对 x 迪斯可》[FLAC/分轨][380.78MB]
- 证声音乐图书馆《星空下 爵士钢琴》[320K/MP3][65.88MB]
- 证声音乐图书馆《星空下 爵士钢琴》[FLAC/分轨][283.95MB]
- 沙门怀一《竹山听雨HQ》头版限量[低速原抓WAV+CUE]
- 沙门怀一《于喁·壹HQ》头版限量编号[低速原抓WAV+CUE]
- 群星《魅音绝唱》黑胶CD【WAV】
- 腾格尔.2002-四十独白【风潮】【WAV+CUE】
- 陈明真.1992-到哪里找那么好的人【华星】【WAV+CUE】
- 黄凯芹.2012-廿五年3CD【环球】【WAV+CUE】
- 证声音乐图书馆《七夕 爵士情缘》[320K/MP3][64.8MB]
- 证声音乐图书馆《七夕 爵士情缘》[FLAC/分轨][327.79MB]