根据对应文件修改图片(等)的名称
使用场景:适合大批量文件名修改工作.前提:有现成或者半成品的对应文件及需要被修改的图片(其他文件等)
限制条件:对应文件目前只支持TXT.(大神们有时间的话可帮忙修改成可支持EXCEL的,不胜感激!!!)
源代码如下:
import easygui as eg
import os
import os.path as op
import datetime as dt
# 获取对应文件的内容存放于all_filename_dict字典中,对应文件本身重复的信息存放在existin_filename_list 中并发挥
def Getpath(file_path):
# 定义存放对应信息的字段
all_filename_dict = {}
# 定义存放重复信息的列表
existin_filename_list = []
# 按照路径打开对应文件并进行遍历
try:
with open(file_path) as file_txt:
for each_filename in file_txt:
(filename_old, filename_new) = each_filename.split(',', 1)
if filename_old in all_filename_dict.keys():
existin_filename_list.extend([]])
else:
all_filename_dict = filename_new.splitlines()
except OSError as reason:
print('出错了!!!', str(reason))
# print('A-', existin_filename_list)
#
# for i in all_filename_dict:
# print('B-原文件名是:%s,修改后文件名是:%s' % (i, all_filename_dict))
# for ii in sorted(existin_filename_list):
# print('C-(重复文件,注意警戒)原文件名是:%s,修改后文件名是:%s' % (ii, ii))
return all_filename_dict, existin_filename_list
# 根据对应文件的内容关系,修改文件对象的文件名
def UpdateObject(file_object_list, all_filename_dict, file_object_path):
# 定义:对象文件在对应文件中匹配不到信息(照片找不到个人信息)
noexist_fileobjet = []
# 定义:对象文件按照规则修改后的名字重复(同一学籍号对应多个人员信息)
existin_fileobjet_list = []
# 定义:文件对象列表
all_object_list = []
# 定义:对应文件中的信息找不到文件对象(无照片人员信息)
all_filename_exist = []
# 更改当前路径为文件对象的路径
os.chdir(file_object_path)
# 遍历文件对象列表,判断
for each_odject in file_object_list:
(each_odject_filename, each_odject_type) = op.splitext(each_odject)
all_object_list.append(each_odject_filename)
if each_odject_filename in all_filename_dict.keys():
try:
# 修改文件名
os.rename(each_odject, all_filename_dict + each_odject_type)
except:
# print('E-文件对象%s按照对应文件中规则修改后的名字%s重复' % (
# each_odject, all_filename_dict + each_odject_type))
existin_fileobjet_list.append(each_odject)
os.rename(each_odject, all_filename_dict + '重复' + each_odject_type)
else:
# print('F-文件对象%s在对应文件中不存在' % each_odject_filename)
noexist_fileobjet.append(each_odject_filename)
# 分析对应文件中的信息找不到文件对象的信息(无照片)
for each_dict_key in all_filename_dict.keys():
if each_dict_key not in all_object_list:
all_filename_exist.append(each_dict_key)
return existin_fileobjet_list, noexist_fileobjet, all_filename_exist
# 问题情况保存到D盘下的文件(文件夹名批量修改日志)里的TXT文件里(文件名为当前系统时间:nowtime)
def SaveLog(existin_filename_list, existin_fileobjet_list, noexist_fileobjet, all_filename_exist, nowTime):
# 定义:问题信息存放的字符串
str_msg = ''
for each_one in existin_filename_list:
str_msg += '对应文件中本身重复(学籍重复):%s\n' % each_one
for each_two in existin_fileobjet_list:
str_msg += '对象文件按照规则修改后的名字重复(同一学籍号对应多个人员信息):%s\n' % each_two
for each_three in noexist_fileobjet:
str_msg += '对象文件在对应文件中匹配不到信息(照片找不到个人信息):%s\n' % each_three
for each_four in all_filename_exist:
str_msg += '对应文件中的信息找不到文件对象(无照片):%s\n' % each_four
# 固定日志的保存路径
try:
os.mkdir('D://文件夹名批量修改日志')
except:
eg.msgbox('路径:D://文件夹名批量修改日志已存在')
# 创建文件并保存str_msg
try:
with open('D://文件夹名批量修改日志//nowTime' + '.log', 'w') as file_log:
file_log.write(str_msg)
except OSError as reason:
print('出错了!!!', str(reason))
# -----------------------------------------------------------------------------------------------------------------
eg.msgbox(msg='注意事项:\n对应文件字段:原文件名,修改后文件名,用逗号隔开,类型为TXT文件'
'\n对应文件需要仔细检查和整理!!!\n(*不能存在空格,空行等,不符合身份证规则的应该提前处理掉.)', title='文件名修改')
file_path = eg.fileopenbox(title='选择对应文件', msg='TXT', default='', filetypes=["*.txt"])
# -----------------------------------------------------------------------------------------------------------------
(all_filename_dict, existin_filename_list) = Getpath(file_path)
# -----------------------------------------------------------------------------------------------------------------
file_object_path = eg.diropenbox(msg='选择需要修改的文件的存放路径', title='选择路径')
# -----------------------------------------------------------------------------------------------------------------
file_object_list = os.listdir(file_object_path)
# -----------------------------------------------------------------------------------------------------------------
(existin_fileobjet_list, noexist_fileobjet, all_filename_exist) = UpdateObject(file_object_list, all_filename_dict,
file_object_path)
# 获取当前系统时间
nowTime = dt.datetime.now().strftime('%Y-%m-%d')
# -----------------------------------------------------------------------------------------------------------------
SaveLog(existin_filename_list, existin_fileobjet_list, noexist_fileobjet, all_filename_exist, nowTime)
页:
[1]