# coding: utf-8
import os
import sys, time
import threading
def split_list_item_to_group(item_list, group_count):
"""
将传入的List中的元素拆分到多个List中,再将这些List作为一个List返回
:param item_list:
:param group_count:
:return:
"""
item_group_list = list()
for n in range(0, group_count):
item_group_list.append(list())
for num in range(0, len(item_list)):
mod_num = num % group_count
item_group_list[mod_num].append(item_list[num])
return item_group_list
def print_target_with_thread(thread_paras):
target_group = thread_paras["target_group"]
for target in target_group:
# print("target is {0}".format(target))
newFileName = target.strip().replace('<', '《').replace('>', '》').replace('\n', '').replace('\r', '').replace('', '').replace(" ", "")
old_file = r"E:/pdf" + "/" + target
new_file = r"E:/pdf" + "/" + newFileName
if not old_file == new_file:
print("old is {0}".format(old_file))
print("new is {0}".format(new_file))
try:
os.rename(old_file, new_file)
except BaseException:
print("Error: 被重命名文件已存在")
time.sleep(0.1)
def start_rename():
target_list = list()
thread_count = 60
thread_list = list()
# for target in range(0, 40000):
# target_list.append(target)
target_list = os.listdir(r'E:\UploadFiles\flfgk\pdf')
target_group_list = split_list_item_to_group(target_list, thread_count)
for target_group in target_group_list:
thread_paras = {
"target_group": target_group,
}
thread_item = threading.Thread(target=print_target_with_thread, args=(thread_paras,))
thread_list.append(thread_item)
thread_item.start()
running_thread_count = thread_count
while running_thread_count > 0:
running_thread_count = len(filter(lambda item: item.isAlive(), thread_list))
print("正在运行的线程数:{0}".format(running_thread_count))
time.sleep(1)
print("多线程执行完成")
start_rename()
06-25
163
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交