用多进程模块multiprocessing 复制上千个文件,并利用Hash验证拷贝的文件是正确的

一道面试题:
用多进程或者多线程把一个文件夹下面的上千个文件(不同文件类型)拷贝到另一个文件夹下面,并证明你拷贝的文件是正确的
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     copyFile
   Description :  复制多个文件并hash验证
   Author :       Wayne Shaw
   date:          2018/8/09
-------------------------------------------------
   Change Activity:
                   2018/8/09:
-------------------------------------------------
"""
# 1、创建1000多个不同类型文件
import random

filetypeL = ['.txt','.doc','.js','.css','.html','.py','.jar','.csv']
for i in range(1024):
  filetype = random.choice(filetypeL)
  filename = str(i)+filetype
  with open('./hash1000plus/'+filename,'wb') as f:
    f.write(str(i).encode('utf-8'))
# 2、复制文件
import os, sys
from multiprocessing import Pool
from multiprocessing import Manager


# 定义复制文件方法
def copy_file(name, path, new_path, q):
  os.listdir(path)
  with open(path + "/" + name) as f:
    with open(new_path + '/' + name, "w") as nf:
      while True:
        content = f.read(2048)
        if content != '':
          nf.write(content)
        else:
          break
  q.put(name)

def run():
  # 原文件夹路径
  path = "./hash1000plus"
  # 新文件夹路径(含文件夹名)
  new_path = "./copy1000plus"
  # 创建新文件夹
  os.mkdir(new_path)
  filenameL = os.listdir(path)
  pool = Pool(10)
  # 创建队列用
  q = Manager().Queue()
  for name in filenameL:
    pool.apply_async(copy_file, args=(name, path, new_path, q))

  num = 0
  allnum = len(filenameL)
  # 设置复制结束条件,打印显示复制总进度
  while num < allnum:
    q.get()
    num += 1
    copyrate = int(num / allnum)
    sys.stdout.write("\r 进度为:%.2f%%" % (copyrate * 100))
    # 将缓冲区中的数据立刻写入文件,同时清空缓冲区
    sys.stdout.flush()


if __name__ == '__main__':
  run()
# 3、对文件夹每个文件做hash,验证文件复制是否成功
import hashlib
import os

# 定义对文件做hash的方法
def hashFile(filename):
  h = hashlib.sha256()
  with open(filename, 'rb') as f:
    while True:
      chunk = f.read(2048)
      if not chunk:
        break
      h.update(chunk)
  return h.hexdigest()

fList = os.listdir('./hash1000plus')
result = 1
for file in fList:
  if hashFile('./hash1000plus/'+file) != hashFile('./copy1000plus/'+file):
    print(file+":复制不成功!")
    result = 0
    break
if result != 0:
  print('文件都已复制成功!')

 

 

 

你可能感兴趣的:(Python学习)