Python 多執行緒技術拷貝檔案

2021-10-02 20:14:58 字數 991 閱讀 9323

使用多執行緒拷貝乙個目錄,要求同時拷貝該目錄下的這些檔案

import os

from threading import thread

# 先定義拷貝函式

defcopy

(dir_from, dir_to,

file):

# 拷貝檔案

if os.path.isfile(

"%s/%s"

%(dir_from,

file))

: f =

open

("%s/%s"

%(dir_from,

file),

"rb"

) f_new =

open

("%s/%s"

%(dir_to,

file),

"wb"

) data = f.read(

) f_new.write(data)

# 主程式封裝成函式

defmain

(dir_from, dir_to)

:# 使用列表儲存每個執行緒物件

jobs =

# 遍歷目錄下的每個檔案

forfile

in os.listdir(dir_from)

:# 建立執行緒物件

t = thread(target=copy, args=

(dir_from, dir_to,

file))

# 儲存執行緒物件

# 開始執行緒

t.start(

)# **執行緒

for i in jobs:

i.join(

)if __name__ ==

'__main__'

: main(

"../important"

,"/home/max/備份"

)

多執行緒拷貝檔案

多執行緒拷貝檔案的步驟 宣告乙個帶有 原始檔,目標檔案,當前執行緒拷貝開始位置,當前執行緒拷貝結束位置 這4個引數的構造器 拷貝執行緒中需要實現 1.統計當前執行緒的拷貝進度 2.使用randomaccessfile獲取輸出流,然後使用seek 方法跳過需要讀寫位元組數 3.while迴圈的條件要加...

python多執行緒實現資料夾拷貝

import threading import os import shutil 建立拷貝任務 def copy work source dir,dest dir,file name 拼接檔名路徑 source file path source dir file name dest file pat...

多執行緒技術

1,程序 執行緒 程序 系統中同時執行的不同程式 執行緒 程式中同時執行不同的操作 單個cpu只能按順序執行指令,cpu可以隨機在不同的程序和執行緒進行切換,保證程序和執行緒都執行一遍後再重複這個過程。因為cpu執行速度足夠快,讓人感覺程式是同時執行的。2,執行緒 thread thread sle...