文件夹下文件重命名-多线程

# 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()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值