我有以下代码将文件夹foler_test
复制到文件夹destination_folder
,因为我使用了功能shutil.copytree()
,并且为了显示进度,我使用了QProgresBar
,但是如您在图像中看到的,进度尚未完成:
其他信息:
folder_test
文件夹的总价值为2131094117
但是在终端执行脚本时,它现在抛出的值是2384493856
这是cmd显示的内容:
C: \ Users \ Angel \ Desktop \ folder_test ------ Result getUbication
2131094117 ------- total_size
C: \ Users \ Angel \ Desktop \ folder_test .-------------- ubicatino
start copy
0 partial
98304 partial
65699748 partial
192151368 partial
382959340 partial
643956368 partial
976715316 partial
1373437400 partial
1841905020 partial
2384493856 final_copydo
finished
应该在cmd中完成了该过程,但是QProgressBar
尚未完成
希望您能帮助我
code.py
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import uic,QtCore
import os,shutil
import threading
class TheThread(QtCore.QThread):
ActionFinished = QtCore.pyqtSignal(bool)
ActionValue = QtCore.pyqtSignal(int)
def __init__(self,origin,destination,sizeOrigin):
QtCore.QThread.__init__(self)
self.origin = origin
self.destination = destination
self.sizeOrigin = sizeOrigin
def run(self):
self.copy()
self.ActionFinished.emit(True)
def copy(self):
print("start copy")
final_total = 0
#print(final_total,"final_total",self.sizeOrigin,"total_size_copied")
while final_total<self.sizeOrigin:
print(final_total,"parcial")
for source,dirs,files in os.walk(self.destination):
for f in files:
fp = os.path.join(source,f)
final_total = os.path.getsize(fp)+final_total
self.ActionValue.emit(int(final_total*100/self.sizeOrigin))
QtCore.QThread.msleep(100)
print(final_total,"final_copiado")
class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
uic.loadUi("test.ui",self)
self.getUbication()
def getUbication(self):
target = 'folder_test'
initial_folder = "C:\\"
self.folder_ubication = ""
for root,dirs,files in os.walk(initial_folder):
if target in root:
self.folder_ubication = os.path.join(root)
break
print(self.folder_ubication,"------Result getUbication")
self.getSizeOrigin(self.folder_ubication,self.folder_ubication)
def getSizeOrigin(self,ubication,ubication2):
total_size = 0
for ubication,dirs,files in os.walk(ubication):
for f in files:
fp = os.path.join(ubication,f)
total_size += os.path.getsize(fp)
print(total_size,"-------total_size")
self.startThread(total_size,ubication,ubication2)
def startThread(self,total_size,ubication,ubication2):
#print(ubication,"value",ubication2)
destination = 'C:/Users/Angel/Desktop/destination_folder/folder_test'
t = threading.Thread(target=self.CopyTree,args=(ubication2,destination,))
t.start()
self.copyThread = TheThread(ubication2, destination, total_size)
self.copyThread.ActionValue.connect(self.updateProgressBar)
self.copyThread.ActionFinished.connect(self.FunctionFinish)
self.copyThread.start()
def CopyTree(self,ubication2,destination):
print(ubication2,".--------------ubicatino")
shutil.copytree(ubication2, destination)
def updateProgressBar(self,progress):
self.Bar.setValue(progress)
def FunctionFinish(self,state):
print("finished")
app = QApplication([])
m = Main()
m.show()
app.exec_()
test.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>565</width>
<height>353</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QProgressBar" name="Bar">
<property name="geometry">
<rect>
<x>160</x>
<y>180</y>
<width>118</width>
<height>23</height>
</rect>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>565</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
新信息
然后为什么QProgressBar
停止并且复制的文件的大小大于原始文件的大小