pyqt——单个以及批量文件备份(复制)及进度条显示

pyqt备份文件(及文件夹)QprogressBar显示

from os.path import join, getsize
from PyQt5.QtCore import(pyqtSolt,QThread)

def update_progress(self, progress):
	"""更新进度条函数"""
	self.ui.progressBar.setValue(progress)
@pyqtSlot()
def on_btnCopy_clicked(self):
	"""点击开始拷贝函数"""
	copy_thread = CopyTread(self, Scr, Des)
	copy_thread.proPartDone.connect(self.update_progress)
	copy_thread.start()
	
class CopyTread(Qthread):
	procPartDone = QtCore.pyqtSignal(int)
	
	def __int__(self, parent, source, destination):
		QTread.__int__(self, parent)
		self.source = source
		self.destination = destination
	
	def run(self):
		self.copy()
	
	def copy(self):
		source_size = 0
		copied = 0  # 设置初始拷贝值
		pathnames = []
		for(root, dir, files) in os.walk(self.source):
			# source_size = os.stat(self.source).st_size  # 计算单个文件夹内存大小
			source_size += sum([getsize(join(root, name)) for name in files])  # 计算文件夹内存总大小
			for file in files:
				pathnames += [os.path.join(root, file)]  # 全部文件路径追加到列表
		for i, filename in enumerate(pathnames):
			source = open(silename, 'rb')
			target = open(Des + '//' + os.path.split(filename)[1], 'wb')
			while True:
				chunk = source.read(1024)
				if nor chunk:
					break
				target.write(chunk)
				copied += len(chunk)
				self.proParDone.emit(copied * 100 / source_size)  # 格式化进度条大小
posted @ 2021-12-10 16:20  poowicat  阅读(416)  评论(0编辑  收藏  举报