python核心 多程序copy檔案

2021-08-19 07:14:00 字數 1195 閱讀 8258

# 1.獲取要copy的資料夾的姓名

# 2.建立乙個資料夾

# 3.獲取old資料夾中的所有的檔案名字

# 4.使用多程序的方式copy原資料夾中的所有檔案到新資料夾中

# 匯入需要的模組

from multiprocessing import pool, manager

import os

def copyfiletask(name, oldfoldername, newfoldername, queue):

# 完成copy乙個檔案的功能

fr = open(oldfoldername + '/' + name)

fw = open(newfoldername + '/' + name, 'w')

content = fr.read()

fw.write(content)

fr.close()

fw.close()

queue.put(name)

def main():

# 獲取要copy的資料夾名字1

oldfoldername = input("請輸入資料夾的名字:")

# 建立乙個資料夾

newfoldername = oldfoldername + '-復件'

os.mkdir(newfoldername)

# 獲取old資料夾中所有的檔案名字

filenames = os.listdir(oldfoldername)

# 使用多程序的方式copy原資料夾中的所有檔案到資料夾中

pool = pool(5)

queue = manager().queue()

for name in filenames:

num = 0

allnum = len(filenames)

while true:

queue.get()

num += 1

copyrate = num/allnum

print('copy的進度為:%.2f%%' %(copyrate*100), end='')

if num == allnum:

print('複製完成')

break

if __name__=='__main__':

main()

Python多程序Copy檔案

import os import time def readallfiles dirname 獲取指定目錄的所有內容 all files 1.盤頓目錄是否存在?if os.path.exists dirname 依次遍歷目錄裡面的所有內容 for root,dirs,files in os.walk...

多程序目錄檔案copy器python

乙個多程序拷貝某個目錄下的檔案的程式 有進度顯示 import os import multiprocessing defcopy file q,file name,old folder name,new folder name 完成檔案的複製的任務 print 模擬拷貝檔案 s s 檔名 s ol...

python多程序 python多程序

當有多個非相關任務需要處理時,並行能大大提高處理速度。這裡簡要介紹python的multiprocessing模組。簡單多程序編寫 當我們任務數量確定而且比較少的時候,可以手動為每個任務指定乙個程序來執行。import multiprocessing as mp def f a print a if...