import os
from threading import Thread,Lock
urls = ['/home/桌面/',
'/home/文档/',
'/home/音乐/',
'/home/下载/',
'/home/视频/',
]
lock = Lock()
filename = input('要下载的文件:')
explorer = []
for i in urls:
if os.path.exists(i+filename):
explorer.append(i+filename)
num = len(explorer)
if num == 0:
print('没有资源')
os._exit(0)
size = os.path.getsize(explorer[0])
block_size = size // num + 1
fd = open(filename,'wb')
def load(path,num):
f = open(path,'rb')
seek_types = block_size * n
f.seek(seek_types)
size = block_size
lock.acquire()
fd.seek(block_size * num)
while True:
if size < 1024:
data = f.read(size)
fd.write(data)
break
else:
data = f.read(size)
fd.write(data)
size -= 1024
lock.release()
n = 0
jobs = []
for path in explorer:
t = Thread(target=load,args=(path,n))
jobs.append(t)
t.start()
n += 1
for i in jobs:
i.join()