python multi process file download transfer function

  • 2020-11-20 06:10:12
  • OfStack

This article shares the specific code of python multi-process file download and transmission function for your reference. The specific content is as follows

Requirements:

Implement folder copy function (including files within files), and print copy progress

Module:

os module
multiprocessing module

Code:


import multiprocessing
import os


def deal_file(old_dir,new_dir,file_name,queue):
  #  Open to save file 
  old_file = open(os.path.join(old_dir,file_name),"rb")
  #  Create a new file 
  new_file = open(os.path.join(new_dir,file_name),"wb")
  #  The loop writes the contents to the new file 
  while True:
    #  A single reading 
    data = old_file.readline()
    #  If something is written otherwise exit 
    if data:

      new_file.write(data) 
    else:
      break

  #  Close the file 
  old_file.close()
  new_file.close()
  #  Add a value to the queue to count , Parameter can be filled with any value 
  queue.put(file_name)

def main():
  #  Create a backup of user input files 
  old_dir = input(" Please enter the file name :")

  new_dir = " The backup "+old_dir

  os.mkdir(new_dir)
  #  Lists the files in the folder 
  file_list = os.listdir(old_dir)

  queue = multiprocessing.Queue(128)
  for file_name in file_list:

    pro = multiprocessing.Process(target=deal_file,args=(old_dir,new_dir,file_name,queue))
    #  Create child process 
    pro.start()
  #  Define variables to count 
  num = 0
  while True:
    #  Fetch the value from the queue 
    queue.get()
    #  counter +1
    num += 1
    #  Print download schedule 
    print("\r File downloaded %.2f %%" % (num/len(file_list)*100),end="")
    #  Prompt to complete exit if count equals file length 
    if num == len(file_list):
      print(" File download completed ")
      break
if __name__ == "__main__":

  main()

Related articles: