pyhon 批量複製 移動檔案或者資料夾

2021-09-18 01:16:34 字數 3756 閱讀 2678

前沿:在之前的部落格分享中,已經分享了 python–使用執行緒–批量檔案的移動 關於兩層檔案的移動和複製,但是如果我們想多層的複製例如:d:\department_data_management裡邊的而且包含很多曾資料夾,我們想原封不動的複製到f盤,此時怎麼搞?

今天就分享一下**,使用技術:執行緒+佇列。

# @time    : 2019-04-11 09:59

# @author : xgp

# @file : file_move.py

# @software: pycharm

# @description:把整個資料夾以及子資料夾複製到某個位置。

"""waring:當你複製的檔案比較少的時候,可能因為資料夾只有乙個執行緒在執行,而檔案的複製是很多執行緒在執行,

這樣可能導致資料夾未建立而檔案卻在複製而報錯,這個時候建議在執行一次即可。所以此方法適合複製大量的檔案,或者資料夾。

"""import os

import shutil

import threading

from queue import queue

class filemove:

def __init__(self):

"""一些引數的輸入

"""self.folder_queue = queue()

self.file_queue = queue()

self.method = input('請選擇檔案是選擇 複製 還是移動 ?eg:[複製,移動]:')

self.file_path = input(r'請輸入想要操作的資料夾eg:[d:\image]:')

self.d_path = input(r'請輸入想要 移動\複製 到的碟符或者路徑eg:[e:, e:\image,f:,f:\image\picture]:')

self.s_path = self.file_path[:2]

def write_info(self, file_path):

"""向佇列中寫入資料夾和檔案

:param file_path:

:return:

"""try:

all_folder = os.listdir(file_path)

except filenotfounderror as e:

print('請正確輸入資料夾的位置.......')

else:

for each_file in all_folder:

if os.path.isdir(os.path.join(file_path, each_file)):

self.folder_queue.put(os.path.join(file_path, each_file))

elif os.path.isfile(os.path.join(file_path, each_file)):

self.file_queue.put(os.path.join(file_path, each_file))

def read_folder(self):

"""獲取到資料夾,並在目的盤 建立資料夾,如果資料夾中還含有資料夾和檔案 呼叫 write_info 方法繼續寫。

:return:

"""while true:

try:

folder_path = self.folder_queue.get(timeout=3)

self.folder_queue.task_done()

except baseexception as e:

print('資料夾的獲取結束,開始下乙個階段檔案的複製............... %s' % str(e))

break

else:

try:

if not os.path.exists(folder_path.replace(self.file_path, self.d_path)):

os.makedirs(folder_path.replace(self.file_path, self.d_path))

except baseexception as e:

print('建立資料夾的時候出現了問題 %s' % str(e))

self.write_info(folder_path)

def read_file(self):

"""從file佇列中拿到檔案 並進行複製或者移動。

:return:

"""while true:

try:

file_path = self.file_queue.get(timeout=3)

self.file_queue.task_done()

except baseexception as e:

print('獲取檔案結束,整個流程走完.........終止 %s' % str(e))

break

else:

try:

if not os.path.exists(file_path.replace(self.file_path, self.d_path)): # 這個是你需要修改的

if self.method == "複製":

print('複製中..........')

shutil.copy(file_path, file_path.replace(self.file_path, self.d_path))

elif self.method == "移動":

print('移動中.........')

shutil.move(file_path, file_path.replace(self.file_path, self.d_path))

shutil.rmtree(self.file_path)

else:

print('暫未開放此功能........敬請期待')

break

else:

print('已經存在了')

pass

except baseexception as e:

print('複製檔案的時候出錯了...............%s' % str(e))

def run(self):

"""主邏輯開始執行。

:return:

"""thread_list =

info_list = threading.thread(target=self.write_info, args=(self.file_path,))

folder_list = threading.thread(target=self.read_folder)

for i in range(50):

file_list = threading.thread(target=self.read_file)

for t in thread_list:

t.setdaemon(true)

t.start()

for q in [self.folder_queue, self.file_queue]:

q.join()

print('結束')

if __name__ == '__main__':

file_move = filemove()

file_move.run()

這只是乙個簡單的 複製 和移動,你也可以在源**的基礎上進行修改和新增,使其功能更強,當然你也可以打包一下分享給你的朋友使用,如果執行****現一點小bug自行解決或者一起討論。

C 批量複製檔案

正題 我是直接新建的乙個aspx並在後台 裡寫的,帖cs using system using system.data using system.configuration using system.collections using system.web using system.web.secu...

C 批量複製檔案

我是直接新建的乙個aspx並在後台 裡寫的,帖cs using system using system.data using system.configuration using system.collections using system.web using system.web.securit...

Python 批量移動檔案

移動檔案 import pandas as pd names pd.read csv sub1009.csv header none names.head import os,shutil defmovefile srcfile,dstfile if not os.path.isfile srcfi...