使⽤pythonos模块复制⽂件到指定⽂件夹的⽅法复制⼀个⽂件夹的⽂件到指定⽬录下
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 = place('.','')
old_name = old_path + '\\' + i
new_name = new_path + '\\' + name + ".json"
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')
numpy库运行速度name = place('.', '')
old_name = old_path + '\\' + i
new_name = new_path + '\\' + name + ".json"
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模块复制⽂件到指定⽂件夹的⽅法就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论