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

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, '秒')

以上这篇使用python os模块复制文件到指定文件夹的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com