使用python os模块复制文件到指定文件夹

复制一个文件夹的文件到指定目录下

import os
import shutil
import time

start_time = time.time()
# 需要被复制的文件夹

old_path = r'D:\zjf_workspace\001-地标、利器、服饰\004文本\json1'
new_path = r'D:\zjf_workspace\001-地标、利器、服饰\004文本\json'
all_list = os.listdir(old_path)
for i in all_list:
    print(i)
    name, suffix = i.rsplit('.json')
    name = name.replace('.','')
    old_name = old_path + '\\' + i
    new_name = new_path + '\\' + name + ".json"
    shutil.copyfile(old_name, new_name)

print(len(all_list))
print(all_list)
end_time = time.time()
print(end_time-start_time,'秒')

优化速度:

import gevent
from gevent import monkey

monkey.patch_all()
import os
import shutil
import time


def copy_file(i):
    global num
    num += 1
    print(i,num)
    old_path = r'D:\zjf_workspace\001-地标、利器、服饰\004文本\baidu_isbn5\新建文件夹\txt'
    new_path = r'D:\zjf_workspace\001-地标、利器、服饰\004文本\百度isbn-json-非selenium5'
    name, suffix = i.split('.json')
    name = name.replace('.', '')
    old_name = old_path + '\\' + i
    new_name = new_path + '\\' + name + ".json"
    shutil.copyfile(old_name, new_name)


if __name__ == '__main__':

    start_time = time.time()

    # 需要被复制的文件夹
    old_path = r'D:\zjf_workspace\001-地标、利器、服饰\004文本\baidu_isbn5\新建文件夹\txt'
    all_list = os.listdir(old_path)
    gevent_list = []
    num = 1
    key_num = 0
    for i in all_list:
        key_num += 1
        if key_num >= 1500:
            gevent.joinall(gevent_list)
            gevent.killall(gevent_list)
            gevent_list = []
            key_num = 0
        gevent_list.append(gevent.spawn(copy_file, i))
        # print(i)

    print(len(all_list))
    # print(all_list)
    gevent.joinall(gevent_list)
    end_time = time.time()
    print(end_time - start_time, '秒')
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页
实付 9.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值