python-多线程拷贝

多次拷贝

import os
import threading


def copy_file(source_path, dest_path):


    with open(source_path,'rb') as soure_file:
        with open(dest_path,'wb') as dest_file:
            while True:
                data = soure_file.read(1024)
                if data:
                    dest_file.write(data)
                else:
                    break

if __name__ == '__main__':
    source_dir = 't1'
    dest_dir = 't2'

    try:
        os.mkdir(dest_dir)
    except:
        pass

    file_list = os.listdir(source_dir)

    for file_name in file_list:
        # copy_file(file_name,source_dir, dest_dir)
        for i in range(5):
            source_path = source_dir + '/' + file_name
            dest_path = dest_dir + '/' + str(i) + file_name
            sub_thread = threading.Thread(target=copy_file,args=(source_path, dest_path))
            sub_thread.start()

单次拷贝

import os
import threading


def copy_file(file_name,source_dir, dest_dir):
    source_path = source_dir + '/' + file_name
    dest_path = dest_dir + '/' + file_name
    with open(source_path,'rb') as soure_file:
        with open(dest_path,'wb') as dest_file:
            while True:
                data = soure_file.read(1024)
                if data:
                    dest_file.write(data)
                else:
                    break

if __name__ == '__main__':
    source_dir = 't1'
    dest_dir = 't2'

    try:
        os.mkdir(dest_dir)
    except:
        pass

    file_list = os.listdir(source_dir)

    for file_name in file_list:
        # copy_file(file_name,source_dir, dest_dir)
        sub_thread = threading.Thread(target=copy_file,args=(file_name,source_dir, dest_dir))
        sub_thread.start()

 

上一篇:F5设备中存在一个Ticketbleed漏洞,可被远程攻击者利用


下一篇:简析 Linux 下的 bashrc 和 profile 的差异